Java Program to Make a Simple Calculator Using switch…case

In this post, we will learn to code the Java Program to Make a Simple Calculator Using switch…case. Let’s understand arithmetic operations in Java and about switch…case in Java Programming Language.

java program to make a simple calculator using switch case

In this program, we will use four basic arithmetic operators and take two operands from the user to make a simple calculator using switch case in Java Programming Language.

Operators will use to make the simple calculator in Java are as follows:

OperatorResult
+Addition of Two Numbers
Subtraction of Two Numbers
*Multiplication of Two Numbers
/Division of Two Numbers

Now, Let’s see the code of the Java Program to Make a Simple Calculator Using switch…case.

Java Program to Make a Simple Calculator Using switch…case

import java.util.*;

public class Main{
    
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the Operator (+,-,*,/) : ");
        char operator = sc.next().charAt(0);
        
        System.out.println("Enter the First Operand : ");
        double first = sc.nextDouble();
        
        System.out.println("Enter the Second Operand : ");
        double second = sc.nextDouble();
        
        double result = 0;
        
        switch(operator){
        case '+':
            result = first + second;
            break;
        case '-':
            result = first - second;
            break;
        case '*':
            result = first * second;
            break;
        case '/':
            result = first / second;
            break;
        }
        
        System.out.println("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
        
    }
    
}

Output 1

Enter the Operator (+,-,*,/) : +

Enter the First Operand : 23

Enter the Second Operand : 45

The Result is : 
 23.0 + 45.0 = 68.0

Output 2

Enter the Operator (+,-,*,/) : /

Enter the First Operand : 23

Enter the Second Operand : 45

The Result is : 
 23.0 / 45.0 = 0.5111111111111111

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the Operator (+,-,*,/) : ");
        char operator = sc.next().charAt(0);
        
        System.out.println("Enter the First Operand : ");
        double first = sc.nextDouble();
        
        System.out.println("Enter the Second Operand : ");
        double second = sc.nextDouble();

In this program, we have taken the input of the operator and stored it in the variable operator of char data type by using the sc.next().charAt(0) function of the Scanner Class in Java. This is how we take input of character.

Then, we took the input of both the operands and stored it in the variable named first and second respectively using the Scanner class.

        double result = 0;
        
        switch(operator){
        case '+':
            result = first + second;
            break;
        case '-':
            result = first - second;
            break;
        case '*':
            result = first * second;
            break;
        case '/':
            result = first / second;
            break;
        }

Then, using the switch case in Java we created the simple calculator.

switch() – A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for a switch case.
When the operator matches a case, the statements following that case will execute until a break statement is reached.

        System.out.println("The Result is : \n "+first+" "+operator+" "+second+" = "+result);

Then, we displayed the result using the System.out.println() function in Java.

“\n” is used to change line.

This is the Java Program to Make a Simple Calculator Using switch…case.

Read more : C Program to Make a Simple Calculator Using switch…case

Conclusion

I hope after going through this post, you understand Java Program to Make a Simple Calculator Using switch…case.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *