C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Menu-driven Basic Calculator using switch - Java Program

import java.util.Scanner;

 

public class MenuDrivenCalculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.println("====== BASIC CALCULATOR ======");

        System.out.println("1. Addition");

        System.out.println("2. Subtraction");

        System.out.println("3. Multiplication");

        System.out.println("4. Division");

        System.out.println("5. Modulus");

        System.out.print("Enter your choice (1-5): ");

       

        int choice = sc.nextInt();

 

        System.out.print("Enter first number: ");

        double num1 = sc.nextDouble();

 

        System.out.print("Enter second number: ");

        double num2 = sc.nextDouble();

 

        double result;

 

        switch (choice) {

            case 1:

                result = num1 + num2;

                System.out.println("Result = " + result);

                break;

 

            case 2:

                result = num1 - num2;

                System.out.println("Result = " + result);

                break;

 

            case 3:

                result = num1 * num2;

                System.out.println("Result = " + result);

                break;

 

            case 4:

                if (num2 == 0) {

                    System.out.println("Error! Division by zero is not allowed.");

                } else {

                    result = num1 / num2;

                    System.out.println("Result = " + result);

                }

                break;

 

            case 5:

                result = num1 % num2;

                System.out.println("Result = " + result);

                break;

 

            default:

                System.out.println("Invalid choice! Please choose between 1–5.");

        }

 

        sc.close();

    }

}

Output

 
OUTPUT 1: Addition
====== BASIC CALCULATOR ======
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 1
Enter first number: 10
Enter second number: 5
Result = 15.0
 
OUTPUT 2: Division with non-zero
Enter your choice (1-5): 4
Enter first number: 20
Enter second number: 4
Result = 5.0

OUTPUT 3: Division with Zero
Enter your choice (1-5): 4
Enter first number: 10
Enter second number: 0
Error! Division by zero is not allowed.


OUTPUT 4: Invalid choice
Enter your choice (1-5): 7
Enter first number: 12
Enter second number: 4
Invalid choice! Please choose between 1–5.

Explanation

1. Display menu options

User sees the operations available (Add, Subtract, Multiply, etc.)

2. Read user choice

int choice = sc.nextInt();

This determines which case in the switch executes.

3. Input two numbers

double num1 = sc.nextDouble();

double num2 = sc.nextDouble();

4. Use switch-case

Each case corresponds to an arithmetic operation:

  • Case 1: Addition
  • Case 2: Subtraction
  • Case 3: Multiplication
  • Case 4: Division (with zero check)
  • Case 5: Modulus

5. Handle invalid choice

If user enters a number other than 1–5, default case runs.

6. Display result

Using:

System.out.println("Result = " + result);