In this tutorial you will learn about the Java Program to Make a Simple Calculator Using switch case and its application with practical example.
In this tutorial, we will learn to create a Java program to make a simple Calculator using Switch case using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output
- Class and Object.
- Basic Java programming.
- if-else statements.
- Switch case.
- Nested if-else Statements.
- Java Scanner class.
What is calculator?
Calculator is a electronic device that is used to performing arithmetic operations on numbers.The Calculators do Addition, Subtraction,Multiplication, and Division… and function like that.
Java Program to Make a Simple Calculator Using switch case
In this our program we will create a small calculator using switch case . We would first declared and initialized the required variables. Next, we would prompt user to input the two values . Later in the program we will ask a operator and values to perform that particular operation in our program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Scanner; public class Calculator { public static void main(String[] args) { double a,b,result; // Declaring a variables Scanner s = new Scanner(System.in); System.out.print("Enter first number value:"); a = s.nextDouble(); // getting values from user... System.out.print("Enter second number value:"); b = s.nextDouble(); System.out.print("Enter any operator (+, -, *, /): "); char opt = s.next().charAt(0); // taking operator switch(opt) // using switch case to create Caluclator { case '+': result = a + b; break; // condition for addition case '-': result =a - b; break; // condition for subtraction case '*': result = a * b; break; // condition for Multiplicatin case '/': result = a / b; break; // condition for Divison default: System.out.printf("You have entered wrong operator or value"); return; } System.out.println(a+" "+opt+" "+b+": "+result); } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- a= it will hold First value.
- b= it will hold second value.
- opt=it will hold operator value.
And in the next statement user will be prompted to enter two values and which will be assigned to variable ‘a‘ and ‘b‘ respectively.
And after taking the values form user we will ask to input symbol to perform following tasks.
After taking both the values of variable and opt we will pass the value of opt to switch case to Calculate values
The above statements compute the addition of two numbers and print the output.
Finally, with the break statement compiler ends the execution of program at switch statement.And we can perform different operation given different different operators and different values in our program as shown above in our program.