Menu Driven Calculator Program in C language using Switch Case and Infinite do while loop.

Spread the love
menu-driven-calculator-program-in-c-programming-using-switch

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.

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:

Output:

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.

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:

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.

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.

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Must Practise: Menu Driven Calculator Program using Switch Case and Do while […]

Leave a Reply