Java Program to Make a Calculator Using switch case
What are Switch Case Statements?
In Java, a switch statement is used to perform different actions based on different conditions. The switch statement compares the value of a variable (or an expression) to a list of possible cases, and when a match is found, the code associated with that case is executed. The switch statement can be used as an alternative to a series of if-else statements.
In this Article, we will write a program to to make a Calculator Using switch case.
Switch statements:
A switch case statement in Java is a control flow statement that allows you to choose between multiple different actions based on the value of an expression. The expression is evaluated and the corresponding case that matches the value of the expression is executed.
Basic syntax :
switch (expression) { case value1: // code to be executed if expression == value1 break; case value2: // code to be executed if expression == value2 break; ... default: // code to be executed if expression does not match any of the cases }
The expression is evaluated and compared to the case values. If a match is found, the code block associated with that case is executed. If no match is found, the code block associated with the default case is executed (if one is provided).
It’s important to use the break statement after each case block to exit the switch statement and prevent the execution of any following cases. If you don’t use the break statement and a match is found, all of the following cases will be executed as well.
Program of Simple Calculator using Switch Case :
import java.util.*; public class Main{ public static void main(String[] args){ // Scanner class to take input Scanner scn = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scn.nextInt(); System.out.print("Enter second number: "); double num2 = scn.nextDouble(); // Input value of operator System.out.print("Enter an operator (+, -, *, /): "); char operator = scn.next().charAt(0); // initializing double variable for result double result; switch (operator) { // Case block for + operator case '+': result = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + result); break; // Case block for - operator case '-': result = num1 - num2; System.out.println(num1 + " - " + num2 + " = " + result); break; // Case block for * operator case '*': result = num1 * num2; System.out.println(num1 + " * " + num2 + " = " + result); break; // Case block for / operator case '/': if (num2 != 0) { result = num1 / num2; System.out.println(num1 + " / " + num2 + " = " + result); } else { System.out.println("Cannot divide by zero"); } break; // Default block if no case block is executed default: System.out.println("Invalid operator"); } } }
Output for ‘+’ Case :
Output: Enter first number: 10 Enter second number: 20 Enter an operator (+, -, *, /): + 10.0 + 20.0 = 30.0
Output for ‘-‘ Case :
Output: Enter first number: 20 Enter second number: 10 Enter an operator (+, -, *, /): - 20.0 - 10.0 = 10.0
Output for ‘*’ Case :
Output: Enter first number: 10 Enter second number: 20 Enter an operator (+, -, *, /): * 10.0 * 20.0 = 200.0
Output for ‘/’ Case :
Output: Enter first number: 20 Enter second number: 10 Enter an operator (+, -, *, /): / 20.0 / 10.0 = 2.0
In this program, the user is prompted to enter two numbers and an operator. The operator is then compared to the cases ‘+’, ‘-‘, ‘*’, and ‘/’ in the switch statement. When a match is found, the corresponding operation is performed and the result is printed to the console. If an invalid operator is entered, the code associated with the default case is executed and an error message is displayed.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment