Menu Driven Calculator Program in C language using Switch Case and Infinite do while loop.
Menu Driven Calculator Program Description:
This is a Menu Driven calculator program, We are going to use the switch case and the Infinite do-while loop to create a calculator. So the program displays a Menu with the Options.
The Menu Options are the operations you want to perform, Like 1 for Addition, 2 for subtraction, etc.
1 2 3 4 5 6 7 |
***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit |
The Program waits for the user’s input, User can provide any numeric option based on Menu.
Select a Number (1-5):
After that, the program prompts the user to provide Two Numbers to carry out the chosen operation.
Enter Two Numbers:
Once the user provided the numbers, The chosen operation is performed, and Program displays the result on the console.
The program will display the Menu again to accommodate the user’s new request.
If the user selects the Invalid option, Program will display an Error message and presents the Menu again.
The User can exit the program by specifying Option 6
Example Input and Output:
Input:
1 2 3 4 5 6 7 8 9 |
***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 1 Enter Two Numbers: 50 50 |
Output:
1 |
Addition of 50 + 50 = 100 |
Pre-Requisites:
It is recommended to have knowledge of the switch statement and do while loop, arithmetic operators. Please go through the following articles to learn more about them.
- Arithmetic Operators.
- Switch Statement in C Language
- do while loop in Language with example programs
Menu Driven Calculator Program in C Explanation:
- As you can see from the above-excepted output, We display the Menu first and then ask for the user input ( Input for Operation to perform and Two Operands i.e Two Numbers)
- Based on user input, We perform the arithmetic operation.
- We used the Infinite do while loop to display the Menu. We used while (1) condition to create the Infinite loop
- We also used the Switch statement to match user input with options.
switch (in)
- If the user enters 1, The switch case with 1 ( case 1: ) will be executed and the program will perform the addition on provided numbers and display the result.
- Similarly, If the user enters 2, The switch statements case 2 will be executed and the program displays the subtraction of given values.
- If the user enters an Invalid Option (Like the options which are not present in the above menu). The default case of the Switch statement will be executed. Which displays the error message Invalid Option, Please select the valid Options on the console.
- If the user selects the Option 6, Then the if statement above the switch case will be executed as the if condition if ( in == 6 ) evaluated as True. So We will break out of the loop.
- The program will continue until the user chooses the exit option. ( 6:Exit )
Menu Driven Calculator Program in C Language:
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 52 53 54 55 56 57 58 59 60 61 |
/*     Program: Menu-driven Calculator program using switch and do while */  #include<stdio.h> int main() {      int num1, num2;     int result;     int in;      do {          printf("\n***** Calculator *****\n");         printf("1. Addition\n");         printf("2. Substraction\n");         printf("3. Multiplication\n");         printf("4. Division\n");         printf("5. Modulo Division\n");         printf("6. Exit\n");         printf("Select a Number (1-5): ");         scanf("%d", &in);          // If 'User' Enters 6 - Stop the program.         if(in == 6) {             printf("Thank you for trying! Bye! \n");             break;         }          // Take two numbers from the user         printf("Enter Two Numbers: ");         scanf("%d%d", &num1, &num2);          switch (in)         {         case 1:             printf("Addition of %d + %d = %d \n", num1, num2, num1+num2);             break;         case 2:             printf("Subtraction of %d - %d = %d \n", num1, num2, num1-num2);             break;         case 3:             printf("Product of %d * %d = %d \n", num1, num2, num1*num2);             break;         case 4:             printf("Division of %d / %d = %d \n", num1, num2, num1/num2);             break;         case 5:             printf("Division of %d %% %d = %d \n", num1, num2, num1%num2);             break;          default:             printf("Invalid Option, Please select the valid Options\n");             break;         }                     } while(1);      return 0; } |
Output: Menu Driven Calculator Demo:
We used the GCC compiler to compile the program learn more at the following URL.
Let’s Compile and run the above program.
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
$ gcc menu-calculator.c $ ./a.out  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 3 Enter Two Numbers: 10 20 Product of 10 * 20 = 200  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 1 Enter Two Numbers: 50 50 Addition of 50 + 50 = 100  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 2 Enter Two Numbers: 764 29 Subtraction of 764 - 29 = 735  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 10 Enter Two Numbers: 10 20 Invalid Option, Please select the valid Options  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 5 Enter Two Numbers: 20 3 Division of 20 % 3 = 2  ***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 6 Thank you for trying! Bye! $ |
As you can see from the above output, The program displayed the Menu and the user had the option to choose the operation. Based on the user option and given numbers the arithmetic operation is performed.
Also Note, That program is able to handle the Invalid Options. by showing the error message Invalid Option, Please select the valid Options
Finally, We can exit the program by using the Option 6 , which terminates the loop.
1 2 3 4 5 6 7 8 9 10 |
***** Calculator ***** 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo Division 6. Exit Select a Number (1-5): 6 Thank you for trying! Bye! $ |
2 Responses
[…] Must Practise: Menu Driven Calculator Program using Switch Case and Do while […]
[…] Must Practise: Menu Driven Calculator Program using Switch Case and Do while […]