Java program to make a simple calculator using switch...case

www.igi‮itf‬dea.com

Here's an example Java program to make a simple calculator using the switch...case statement:

import java.util.Scanner;

public class SimpleCalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter two numbers: ");
        double num1 = input.nextDouble();
        double num2 = input.nextDouble();

        System.out.println("Enter an operator (+, -, *, /): ");
        char operator = input.next().charAt(0);

        double result;

        switch(operator) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            default:
                System.out.println("Error! Invalid operator.");
                return;
        }

        System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
    }
}

This program prompts the user to enter two numbers and an operator, and then performs the appropriate arithmetic operation based on the operator using a switch...case statement. The result is printed to the console.

For example, if the user enters 2, 3, and "+", the program will output:

2.0 + 3.0 = 5.0