Menu Driven Calculator Program in Java | Switch-case

Hello Friends, Welcome to my new tutorial. In this tutorial, we will learn to create menu driven calculator program in Java. Using this Menu Driven calculator program, The user will have the option to choose and perform any of the basic arithmetic operations like addition, subtraction, Multiplication, Division etc. It can also be used for finding the square, square root and reciprocal of any number.

Before starting this tutorial, you need to have the basic concept of the following topics to understand the tutorial clearly.

I will use Notepad for the coding part of my program, but you are free to use any IDE like NetBeans, Eclipse or IntelliJ Idea because the programming logic and coding will be the same. So without further ado, let’s start our tutorial menu driven calculator program in Java.

Related Articles
Simple Calculator Program in Java Using Swing
Simple Calculator Program in Java Using AWT

Menu Driven Calculator Program in Java Using Switch Case

In this case, we will use the Java switch case statement to build our menu driven calculator program. The Java switch statement will help us to execute one statement from the multiple choices that will be provided to the user. Let’s have a look at the algorithm for our program using a switch-case statement.

Algorithm for Menu Driven Calculator Program in Java using Switch case

Code is following :
What we did?

  • First of all, we have imported the java.util package so that we can use the Scanner class in our program.
  • Then created a class and provided the name MenuDrivenCalculator.
  • Then created the main method.
  • Inside main method declared all the variables that we used later in our program.
  • After that, we have created the object of the Scanner class.
  • Then started the do-while loop. The do-while loop is an exit controlled loop, and in this type of loop, First of all the loop body gets executed, and then the condition is checked while exiting from the loop body. The loop body repeatedly gets executed till the condition is satisfied, and the loop terminates as soon as the condition becomes false.
  • Then displayed a Menu.
  • After that, asked the user to enter their choice.
  • Then created the switch case branch. The first case is for finding the addition of two numbers, the second case is for finding the difference, the third case is for finding the product, the fourth case is for finding the quotient and remainder, the fifth case is for finding the square, the sixth case is for finding the square root and the seventh case is for finding the reciprocal.
  • If the user enters an invalid choice that is not in the menu, then a message will be displayed. Invalid choice!! Please make a valid choice !!!
  • Then printed a message to continue or exit from the do-while loop and got the user input.
  • If the condition is met to true for the do-while loop, the loop will continue to execute. Otherwise, the program will exit.

Output :

Menu Driven Calculator Program in Java using Switch Case
Menu Driven Calculator Program in Java using Switch Case

Menu Driven Calculator Program in Java Using If-Else

In this case, we will use the ladder of if-else-if to build the menu driven calculator program. Just like the switch case, the if-else-if ladder will help us to execute a particular set of code as per specific conditions, and thus it can be used to build our calculator. If the condition is true, then the associated if block will be executed, and the rest of the ladder will be ignored, and if none of the if condition is true, then the else part will be executed.

Code is following :

What we did?

  • First of all, we have imported the java.util package so that we can use the Scanner class in our program.
  • Then created a class and provided the name MenuDrivenCalc.
  • Then created the main method.
  • Inside main method declared all the variables that we used later in our program.
  • After that, we have created the object of the Scanner class.
  • Then started the do-while loop.
  • Then displayed a Menu.
  • After that, asked the user to enter their choice.
  • Then created the ladder of if-else-if, and as per the user’s input, the associated block will be executed.
  • If the user enters something which is not available in the menu, then the else block will be executed, and the message will be displayed Invalid choice!! Please make a valid choice !!!
  • Then printed a message to continue or exit from the do-while loop and got the user input.
  • If the condition is met to true for the do-while loop, the loop will continue to execute. Otherwise, the program will exit.

Output :

Menu Driven Calculator Program in Java Using if-else
Menu Driven Calculator Program in Java Using if-else

So guys, here I am wrapping up this tutorial Menu Driven Calculator Program In Java. Don’t forget to drop your queries in the comment section if you have any doubts.

People Are Also Reading…

Leave a Comment