Java Program to Make a Simple Calculator Using switch...case


In this article, we will understand how to construct a simple calculator using switch-case. The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.

Following are the arithmetic operations we are going to perform.

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Floor Division
  • Modulo

Below is a demonstration of the same −

Input

Suppose our input is −

The two inputs: 40.0 and 12.0
Operator:%

Output

The desired output would be −

The result is 40.0 % 12.0 = 4.0

Algorithm

Step 1 - START
Step 2 - Declare three values namely my_input_1, my_input_2 and my_result and declare a character value namely operator.
Step 3 - Read the required values from the user/ define the values
Step 4 - Define case statements which takes ‘operator’ value as switch case to calculate the sum, difference, multiplication, division, modulus.
Step 5 - Pass the operator value to the case statements to calculate the arithmetic operation between the two inputs ‘my_input_1’ and ‘my_input_2’
Step 7 - Display the result
Step 8 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class OperatorSwitch {
   public static void main(String[] args) {
      char operator;
      Double my_input_1, my_input_2, my_result;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.println("Enter the first number");
      my_input_1 = my_scanner.nextDouble();
      System.out.println("Enter the second number");
      my_input_2 = my_scanner.nextDouble();
      System.out.println("Enter any of the following operator: +, -, *, /, %");
      operator = my_scanner.next().charAt(0);
      switch (operator) {
         case '+':
            my_result = my_input_1 + my_input_2;
            System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result);
            break;
         case '-':
            my_result = my_input_1 - my_input_2;
            System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result);
            break;
         case '*':
            my_result = my_input_1 * my_input_2;
            System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result);
            break;
         case '/':
            my_result = my_input_1 / my_input_2;
            System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result);
            break;
         case '%':
            my_result = my_input_1 % my_input_2;
            System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result);
            break;
         default:
            System.out.println("The operator you have selected is invalid");
            break;
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the first number
40
Enter the second number
12
Choose any of the following operator: +, -, *, /, %
%
40.0 % 12.0 = 4.0

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class OperatorSwitch {
   public static void main(String[] args) {
      char operator;
      Double my_input_1, my_input_2, my_result;
      my_input_1 = 40.0;
      my_input_2 = 12.0;
      operator = '%';
      System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2);
      System.out.println("The operator is defined as " +operator);
      switch (operator) {
         case '+':
            my_result = my_input_1 + my_input_2;
            System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result);
            break;
         case '-':
            my_result = my_input_1 - my_input_2;
            System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result);
            break;
         case '*':
            my_result = my_input_1 * my_input_2;
            System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result);
            break;
         case '/':
            my_result = my_input_1 / my_input_2;
            System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result);
            break;
         case '%':
            my_result = my_input_1 % my_input_2;
            System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result);
            break;
         default:
            System.out.println("The operator you have selected is invalid");
            break;
      }
   }
}

Output

The two numbers are defined as 40.0 and 12.0
The operator is defined as %
40.0 % 12.0 = 4.0

Updated on: 22-Feb-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements