CodersZone logo

Menu Driven Calculator

Menu-driven program in java is a type of program in which various options are provided to the user and some functionality is provided based on these options. These programs are used to provide a simple yet effective user interface and deliver the requested functionalities to the user. This article provides an overview of menu-driven programs in Java.

This code is an example of a menu driven calculator program, with operations like:

Addition
Subtraction
Multiplication
Division
Exponent
Root

Algorithm

  1. The driver program contains switch case that lets us choose an operation
  2. The addition function adds Two numbers
  3. the subtraction function finds the difference of two numbers
  4. the multiplication function does the product
  5. The division function gives the quotient
  6. Exponent function gives the power of a number
  7. root finds the root to the power of any number

Code

//Importing Saanner class
import java.util.Scanner;
 
//Creating Class
public class Main {
 
    //Creating main method
    public static void main(String[] args){
 
        //Declaring all variables
        int a,b,c;
        int choice;
        Scanner scanner = new Scanner(System.in);
        //Creating infinite while loop
        while(true) {
 
            //Creating menu
            System.out.println("Press 1 for Addition");
            System.out.println("Press 2 for Subtraction");
            System.out.println("Press 3 for Multiplication");
            System.out.println("Press 4 for Division");
            System.out.println("Press 5 to Quit\n \n ");
 
            //Asking user to make choice
            System.out.println("Make your choice");
            choice = scanner.nextInt();
 
            //Creating switch case branch
            switch (choice) {
 
                //First case for finding the addition
                case 1:
                    System.out.println("Enter the first number ");
                    a = scanner.nextInt();
                    System.out.println("Enter the second number");
                    b = scanner.nextInt();
                    c = a + b;
                    System.out.println("The sum of the numbers is = " + c +"\n");
                    break;
 
                //Second case for finding the difference
                case 2:
                    System.out.println("Enter the first number ");
                    a = scanner.nextInt();
                    System.out.println("Enter the second number");
                    b = scanner.nextInt();
                    c = a - b;
                    System.out.println("The difference of the numbers is = " + c +"\n");
                    break;
 
                //Third case for finding the product
                case 3:
                    System.out.println("Enter the first number");
                    a = scanner.nextInt();
                    System.out.println("Enter the second number");
                    b = scanner.nextInt();
                    c = a * b;
                    System.out.println("The product of the numbers is = " + c + "\n");
                    break;
 
                //Fourth case for finding the quotient
                case 4:
                    System.out.println("Enter the first number");
                    a = scanner.nextInt();
                    System.out.println("Enter the second number");
                    b = scanner.nextInt();
                    c = a / b;
                    System.out.println("The quotient is = " + c +"\n");
                    break;
 
                //Fifth case to quit the program
                case 5:
                    System.exit(0);
 
                //default case to display the message invalid choice made by the user
                default:
                    System.out.println("Invalid choice!!! Please make a valid choice. \\n\\n");
            }
        }
    }
}
            

Output

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
1
Enter the first number 
5
Enter the second number
7
The sum of the numbers is = 12

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
2
Enter the first number 
8
Enter the second number
7
The difference of the numbers is = 1

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
3
Enter the first number
8
Enter the second number
7
The product of the numbers is = 56

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
4
Enter the first number
7
Enter the second number
8
The quotient is = 0

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
6
Invalid choice!!! Please make a valid choice. \n\n
Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 to Quit
 
 
Make your choice
5
 
See the same program in C See the same program in Python