Simple calculator program in java using switch case – Java Program to Make a Simple Calculator Using switch-case

Simple calculator program in java using switch case: Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Program to Make a Simple Calculator Using switch-case

Java programs calculator: In this article we will make simple calculator using switch case statement.

Let’s first see the approach of the program.

Approach:

  1. First take two numbers input from the user means two operands, upon which the calculation will be performed.
  2. Store two numbers in double variables operand1 and operand2 respectively.
  3. After taking two operands input now take the operator input from the user, means which operation will be performed.
  4. As operator input the user has to give either one operator among +(addition), -(subtraction), *(multiplication), /(division). And store it in character variable operator.
  5. Now according to the case(operator symbols) perform the respective operation.
  6. Then produce the result as output and with the help of double variable result.

Let’s see the below program to understand it more clearly.

Program:

import java.util.Scanner;

class Main 
{
  public static void main(String[] args) 
  {

    char operator;
    Double operand1, operand2, result;

    // Scanner class object created, for user input
    Scanner sc= new Scanner(System.in);

    // enter first number
    System.out.println("Enter first number : ");
    operand1 = sc.nextDouble();
    // enter second number
    System.out.println("Enter second number : ");
    operand2 = sc.nextDouble();
    
     // asking for operator user input
    System.out.println("Choose an operator : +, -, *, or /");
    operator = sc.next().charAt(0);

    switch (operator) 
    {

      // addition operation
      case '+':
        result = operand1+operand2;
        System.out.println("Result = "+operand1+ " + " +operand2+ " = " + result);
        break;

      // subtraction operation
      case '-':
        result = operand1-operand2;
        System.out.println("Result = "+operand1+ " - " +operand2+ " = " + result);
        break;

      // multiplication operation
      case '*':
        result = operand1*operand2;
        System.out.println("Result = "+operand1+ " * " +operand2+ " = " + result);
        break;

      // division operation
      case '/':
        result = operand1/operand2;
        System.out.println("Result = "+operand1+ " / " +operand2+ " = " + result);
        break;

      default:
        System.out.println("You have entered invalid operator.");
        break;
    }
    System.out.println("THANKS FOR USING BTECHGEEKS CALCULATOR");
    sc.close();
  }
}
Output:

CASE-1 ADDITION

Enter first number : 
605.4
Enter second number : 
200
Choose an operator : +, -, *, or /
+
Result = 605.4 + 200.0 = 805.4
THANKS FOR USING BTECHGEEKS CALCULATOR


CASE-2 SUBTRACTION

Enter first number : 
6789
Enter second number : 
2400
Choose an operator : +, -, *, or /
-
Result = 6789.0 - 2400.0 = 4389.0
THANKS FOR USING BTECHGEEKS CALCULATOR


CASE-3 MULTIPLICATION

Enter first number : 
350
Enter second number : 
30
Choose an operator : +, -, *, or /
*
Result = 350.0 * 30.0 = 10500.0
THANKS FOR USING BTECHGEEKS CALCULATOR


CASE-4 DIVISION

Enter first number : 
24430
Enter second number : 
30
Choose an operator : +, -, *, or /
/
Result = 24430.0 / 30.0 = 814.3333333333334
THANKS FOR USING BTECHGEEKS CALCULATOR

Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.

Related Java Decision Making and Loop Programs :