Hello reader! Are you looking to learn the concept of a Java program to Make Simple Calculator? If yes then you are in the perfect article. Using a calculator we can perform basic mathematical operations such that addition, subtraction, multiplication, and division along with the mathematical operators.
Okay, It’s time to start the article to execute a task.
Java Program to Make Simple Calculator
Here, We can perform the task of “Java program to Make Simple Calculator” using the below program.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// ask users to enter operator
System.out.println("Choose an operator: +, , *, or /");
operator = input.next().charAt(0);
// ask users to enter numbers
System.out.println("Enter first number");
number1 = input.nextDouble();
System.out.println("Enter second number");
number2 = input.nextDouble();
switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
// performs subtraction between numbers
case '':
result = number1  number2;
System.out.println(number1 + "  " + number2 + " = " + result);
break;
// performs multiplication between numbers
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
// performs division between numbers
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}
Execution of Java Program to Make Simple Calculator
Initially, We have to declare the variables to store the input and output data after importing the java.util.scanner() class into this program. Later, We can mention the input variables along with specifying the related arithmetic operator inside the system.out.printIn() function respectively.
After that, We can use the break statement to end the current operation in every arithmetic operation. After running the program, The program asks you to enter the input values using the new scanner() class.
After applying the values, The case statement will check whether the operator is matched or not with the related operation respectively. If it is matched for all operations then the output will display on the screen using the system.out.printIn() function.
Read Also : BMI Calculator Using Java
Note
You can also make a simple calculator using a few other languages like JavaScript program to Make Simple Calculator, C++ Program to Create Simple Calculator, and Python Program to Create Simple Calculator.
Outputs of the Program
Below, I can provide the outputs for each and every operation for your reference.
Output for Addition
Choose an operator: +, , *, or /
+
Enter first number
20
Enter second number
10
20.0 + 10.0 = 30.0
Output for Subtraction
Choose an operator: +, , *, or /

Enter first number
20
Enter second number
10
20.0  10.0 = 10.0
Output for Multiplication
Choose an operator: +, , *, or /
*
Enter first number
20
Enter second number
10
20.0 * 10.0 = 200.0
Output for Division
Choose an operator: +, , *, or /
/
Enter first number
20
Enter second number
10
20.0 / 10.0 = 2.0
Conclusion
I hope this article is about the Java Program to Make Simple Calculator helpful to you. If you face any issues please let me know via the comment section. Share this article with other Java program developers via social networks.