tech Java Program to Implement Calculator Using Switch Case | Java Programs
Latest :

Java Program to Implement Calculator Using Switch Case | Java Programs

Before directly jumping into the solutions, it is advisable to follow a few steps before only to avoid confusion at a later stage. First, we need to understand the problem statement thoroughly. Then, we have to look for any constraints given in the problem. After this, we have to decide on our inputs followed by determining our expected output for this problem. Only after this, we should arrive at a logical solution.

Here, our problem statement is to write a program for a calculator by making use of switch case. Our required inputs are two numbers (num1 and num2) and an operator of our choice using another variable (n). Our expected output is a variable (result) returning the answer based on the choice of operation made and input numbers.

To read inputs at runtime, we will make use of Scanner class in Java for any primitive datatype.  For this, we will create an object for Scanner class which can be used to invoke methods of Scanner class. Prior that, we need to know the operations available for us to perform. If the choice is 1 then, it is addition, it 2 then, subtraction, if 3 then, multiplication, if 4 then, division and if 5 then, modulus.

Scanner sc = new Scanner(System.in);

First, we have to read the choice stored in an integer variable (n). Then we check if the choice made is valid or not. If the value in the variable (n) is between 1 to 5 both included then it is a valid choice and we move to the logic else, we print the choice is invalid and come out of the calculation loop.

int n=sc.nextInt();

if(n>5||n<=0) {

System.out.print(“entered Wrong Choice!\n”);

break;

}

If the choice entered (n) is a valid choice then, we first read the two numbers on which the choice of operation is to be performed using Scanner class as follow:

num1 = sc.nextDouble();

num2 = sc.nextDouble();

After reading the inputs, switch case is performed using the choice variable (n). If it is 1 then, addition is perform and the sum is stored in the resultant variable (result) and comes out of the switch case.

case 1 :

result = num1+ num2;

break;

If the choice is 2 then, the operation to be performed is subtraction. The difference of the two numbers is stored in resultant variable (result) and the execution comes out of the switch case.

case 2 :

result = num1- num2;

break;

If the operation of choice is 3 then, the we have to perform multiplication. The product of the two numbers is stored in the resultant variable (result) and we exit the switch case.

case 3 :

result = num1* num2;

break;

If the choice is 4 then, division has to be performed. For this, we first have to check if divisor is not zero. If it is zero division can’t be performed so we exit the switch case after equating result to zero else, we perform division and store in resultant variable (result) and then exit switch case.

case 4 :

if(num2>0) {

result = num1/ num2;  }

else { System.out.println(“second number should not be 0”);

result=0; }

break;

If the choice is 5 then, we perform the modulus operation. The remainder of the two is stored in the resultant variable (result) and come out of the switch case.

case 5 :

result = num1% num2;

break;

After coming out of switch case, we display the output (result) on our console screen. This is repeated in loop until an invalid choice is entered. Then, we exit the entire while loop and come to the end of our code.

 

Output:

 

tech
x

Check Also

Merge Sort Java – Program 2 Ways | Sortings

Merge Sort Java –  Java program to implement merge sort using array & Buffered reader. ...