Write a Java Program to Make a Simple Calculator Using switch…case

A calculator is a simple yet essential tool that helps us perform basic arithmetic operations like addition, subtraction, multiplication, and division.

In this tutorial, we will write a Java program to make a simple calculator using the switch…case statement.


To begin with, we will declare four variables of type double to store two operands and the result of the operation.

We will also declare a variable of type char to store the operator entered by the user.

Here’s how we can do it:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        double num1, num2, result;
        char operator;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter first number: ");
        num1 = scanner.nextDouble();

        System.out.print("Enter second number: ");
        num2 = scanner.nextDouble();

        System.out.print("Enter operator (+, -, *, /): ");
        operator = scanner.next().charAt(0);

        scanner.close();
    }
}

Next, we will use the switch…case statement to perform the operation based on the operator entered by the user.

We will also handle the division by zero error using a try-catch block.

Here’s how we can do it:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        double num1, num2, result;
        char operator;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter first number: ");
        num1 = scanner.nextDouble();

        System.out.print("Enter second number: ");
        num2 = scanner.nextDouble();

        System.out.print("Enter operator (+, -, *, /): ");
        operator = scanner.next().charAt(0);

        scanner.close();

        switch (operator) {
            case '+':
                result = num1 + num2;
                System.out.println(num1 + " + " + num2 + " = " + result);
                break;

            case '-':
                result = num1 - num2;
                System.out.println(num1 + " - " + num2 + " = " + result);
                break;

            case '*':
                result = num1 * num2;
                System.out.println(num1 + " * " + num2 + " = " + result);
                break;

            case '/':
                try {
                    if (num2 == 0) {
                        throw new ArithmeticException("Division by zero is not allowed");
                    }
                    result = num1 / num2;
                    System.out.println(num1 + " / " + num2 + " = " + result);
                } catch (ArithmeticException e) {
                    System.out.println(e.getMessage());
                }
                break;

            default:
                System.out.println("Invalid operator");
                break;
        }
    }
}

In the above program, we first ask the user to enter two numbers and the operator.

We then use the switch…case statement to perform the operation based on the operator entered by the user.

We also handle the division by zero error using a try-catch block.


In conclusion, this tutorial showed how to write a Java program to make a simple calculator using the switch…case statement.

The switch…case statement is an efficient way to handle multiple conditions and can be used to write clean and concise code.