Start Coding Now
🌱 Beginner Programsbeginner1 methods

Simple Calculator in Java using Switch Case

Create a simple calculator to perform addition, subtraction, multiplication, and division.

Last updated: 11 January 2026

Method 1: Using Switch Case

Menu driven calculator.

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter two numbers: ");
        
        double first = reader.nextDouble();
        double second = reader.nextDouble();
        
        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = reader.next().charAt(0);
        
        double result;
        
        switch(operator) {
            case '+':
                result = first + second;
                break;
            case '-':
                result = first - second;
                break;
            case '*':
                result = first * second;
                break;
            case '/':
                result = first / second;
                break;
            default:
                System.out.printf("Error! operator is not correct");
                return;
        }
        
        System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
    }
}
Output:
Enter two numbers: 10 5
Enter an operator (+, -, *, /): *
10.0 * 5.0 = 50.0

Explanation

Takes two numbers and an operator, then uses switch case to perform the operation.

Frequently Asked Questions

Try This Program

Copy this code and run it in our free online Java compiler.

Open Java Compiler