Write a program to create simple calculator using switch Statement


  • import java.util.Scanner; : This line imports the Scanner class from the java.util package, which is used to read user input.
  • class Calculator: This line declares a new class called Calculator.
  • public static void main(String[] args) : This line defines the main method, which is the entry point for the program.
  • Scanner input = new Scanner(System.in); : This line creates a new Scanner object called input, which reads input from the standard input stream (System.in).
  • char op; : This line declares a char variable called op to store the operator entered by the user.
  • Double n1, n2, res; : This line declares three Double variables called n1, n2, and res to store the two numbers entered by the user and the result of the calculation, respectively.
  • System.out.println("+, -, *, or /");: This line prints a message to the console asking the user to enter an operator.
  • System.out.print("Choose an Operator :"); : This line prints a message to the console asking the user to choose an operator.
  • op = input.next().charAt(0); : This line reads a single character from the input Scanner and assigns it to the op variable.
  • System.out.print("Enter the Number 1 :"); : This line prints a message to the console asking the user to enter the first number.
  • n1 = input.nextDouble();: This line reads a Double value from the input Scanner and assigns it to the n1 variable.
  • System.out.print("Enter the Number 2 :"); : This line prints a message to the console asking the user to enter the second number.
  • n2 = input.nextDouble();: This line reads a Double value from the input Scanner and assigns it to the n2 variable.
  • switch(op) : This line starts a switch statement, which allows the program to execute different code based on the value of the op variable.
  • case '+': : This line defines a case for when the op variable is +.
  • res = n1 + n2; : This line adds the n1 and n2 variables together and assigns the result to the res variable.
  • System.out.println(n1 + " + " + n2 + " = " + res); : This line prints the equation and result of the addition operation to the console.
  • break; : This line ends the case block.
  • case '-': : This line defines a case for when the op variable is -.
  • res = n1 - n2; : This line subtracts the n2 variable from the n1 variable and assigns the result to the res variable.
  • System.out.println(n1 + " - " + n2 + " = " + res); : This line prints the equation and result of the subtraction operation to the console.
  • break; : This line ends the case block.
  • case '*': : This line defines a case for when the op variable is *.
  • res = n1 * n2; : This line multiplies the n1 and n2 variables together and assigns the result to the res variable.

Source Code

import java.util.Scanner;
class Calculator
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		char op;
		Double n1, n2, res;
 
		System.out.println("+, -, *, or /");
		System.out.print("Choose an Operator :");
		op = input.next().charAt(0);
 
		System.out.print("Enter the Number 1 :");
		n1 = input.nextDouble();
		System.out.print("Enter the Number 2 :");
		n2 = input.nextDouble();
 
		switch(op)
		{
			case '+':
				res = n1 + n2;
				System.out.println(n1 + " + " + n2 + " = " + res);
				break;
			case '-':
				res = n1 - n2;
				System.out.println(n1 + " - " + n2 + " = " + res);
				break;
			case '*':
				res = n1 * n2;
				System.out.println(n1 + " * " + n2 + " = " + res);
				break;
			case '/':
				res = n1 / n2;
				System.out.println(n1 + " / " + n2 + " = " + res);
				break;
			default:
				System.out.println("Invalid Operator !");
				break;
		}
	}
}

Output

+, -, *, or /
Choose an Operator :*
Enter the Number 1 :12
Enter the Number 2 :56
12.0 * 56.0 = 672.0