Learn How To Implement Menu Driven Program in C Programming Language. The Menu Driven style is normally used with Switch Case. However, here we have given different ways to implement Menu Driven style which uses Switch Case and Functions. Also, the switch case conversion into If Else Loop is also demonstrated in the code below.
Rules For Switch Case
- The Switch case expression must be an integral type.
- Case labels must be unique and must end with a colon.
- The break statement is mandatory if you want a single case to handle a particular function. Otherwise, you can skip it if you want two cases to work for a single task.
- The default case may be placed anywhere in the switch case. It is optional and can be used only once.
- Case labels must be constant expressions or constants.
Must Read: C Program To Check if a Number is Magic Number or Not
C Program For Menu Driven Program using Switch Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h> int main() { int ch, op1, op2, result; printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n"); printf("Enter First Number:\t"); scanf("%d", &op1); printf("Enter First Number:\t"); scanf("%d", &op2); printf("Enter You Choice:\t"); scanf("%d", &ch); switch(ch) { case 1: result = op1 + op2; printf("Result:\t%d\n", result); break; case 2: result = op1 - op2; printf("Result:\t%d\n", result); break; case 3: result = op1 * op2; printf("Result:\t%d\n", result); break; case 4: result = op1 / op2; printf("Result:\t%d\n", result); break; default: printf("Wrong Option\n"); break; } return 0; } |
Must Read: C Program To Find if a Number is a Strong Number or Not
C Program To Implement Menu Driven Program using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include<stdio.h> int addition(int a, int b) { return (a / b); } int subtraction(int a, int b) { return (a / b); } int multiplication(int a, int b) { return (a / b); } int division(int a, int b) { return (a / b); } int main() { int ch, op1, op2, result; printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n"); printf("Enter First Number:\t"); scanf("%d", &op1); printf("Enter First Number:\t"); scanf("%d", &op2); printf("Enter You Choice:\t"); scanf("%d", &ch); switch(ch) { case 1: result = addition(op1, op2); printf("Result:\t%d\n", result); break; case 2: result = subtraction(op1, op2); printf("Result:\t%d\n", result); break; case 3: result = multiplication(op1, op2); printf("Result:\t%d\n", result); break; case 4: result = division(op1, op2); printf("Result:\t%d\n", result); break; default: printf("Wrong Option\n"); break; } return 0; } |
Must Read: C Program To Check if a Year is a Leap Year or Not
C Program Implementation of Menu Driven program using If Else Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include<stdio.h> #include<stdlib.h> int main() { int ch, op1, op2, result; printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n"); printf("Enter First Number:\t"); scanf("%d", &op1); printf("Enter First Number:\t"); scanf("%d", &op2); while(ch != 5) { printf("Enter You Choice:\t"); scanf("%d", &ch); if(ch == 1) { result = op1 + op2; printf("Result:\t%d\n", result); } else if(ch == 2) { result = op1 - op2; printf("Result:\t%d\n", result); } else if(ch == 3) { result = op1 * op2; printf("Result:\t%d\n", result); } else if(ch == 4) { result = op1 / op2; printf("Result:\t%d\n", result); } else if(ch == 5) { printf("\nExit\n"); exit(0); } } return 0; } |
Must Read: C Program To Print Map of India
Output

If you have any compilation error or doubts in this Implementation of Menu Driven Program using Functions, Switch Case, While Loop and If Else Loop, let us know about it in the comment section below.
Nice. I can now easily convert switch case to if else loop in any programming language. Thanks for this switch case and menu driven implementation.
Perfect explanation. I was looking forward on how to implement switch case using if else block. Thanks.
What is the difference between If and Switch Case? It works almost same? I would prefer to use If Else Blocks since it is easy to use and debug.
hey hi,nice explanation on the if else block….thanks a lot. l have a question on how to pass a function to another function as a a parameter,? is it even possible in c .
Namaste! Thanks for sharing this info. very useful. Esp the following method using IF ELSE
>> C Program Implementation of Menu Driven program using If Else Loop
It is good alternative for Switch – Case.
Cheers!
chandra