Java Program to Create Simple Calculator
A simple Java calculator program uses
Scanner to get two numbers and an operator, then uses switch to perform addition, subtraction, multiplication, or division and prints the result.Examples
Input5 + 3
OutputResult: 8.0
Input10 / 2
OutputResult: 5.0
Input7 * 0
OutputResult: 0.0
How to Think About It
To create a simple calculator, first get two numbers and an operator from the user. Then check which operator was entered using
switch or if statements. Perform the matching arithmetic operation and show the result. Handle division carefully to avoid dividing by zero.Algorithm
1
Get the first number from the user2
Get the second number from the user3
Get the operator (+, -, *, /) from the user4
Use a switch to decide which operation to perform5
Calculate the result based on the operator6
Print the result to the userCode
java
import java.util.Scanner; public class SimpleCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); System.out.print("Enter operator (+, -, *, /): "); char op = scanner.next().charAt(0); double result; switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) { result = num1 / num2; } else { System.out.println("Error: Division by zero"); scanner.close(); return; } break; default: System.out.println("Invalid operator"); scanner.close(); return; } System.out.println("Result: " + result); scanner.close(); } }
Output
Enter first number: 5
Enter second number: 3
Enter operator (+, -, *, /): +
Result: 8.0
Dry Run
Let's trace the input '5', '3', and '+' through the code
1
Read first number
num1 = 5
2
Read second number
num2 = 3
3
Read operator
op = '+'
4
Switch on operator
case '+': result = 5 + 3 = 8
5
Print result
Output: Result: 8.0
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5 |
| 2 | num2 | 3 |
| 3 | op | + |
| 4 | result | 8.0 |
Why This Works
Step 1: Input reading
The program uses Scanner to read two numbers and an operator from the user.
Step 2: Operation selection
It uses a switch statement on the operator to decide which arithmetic operation to perform.
Step 3: Calculation and output
The program calculates the result and prints it. It also checks for division by zero to avoid errors.
Alternative Approaches
Using if-else instead of switch
java
import java.util.Scanner; public class SimpleCalculatorIfElse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); System.out.print("Enter operator (+, -, *, /): "); char op = scanner.next().charAt(0); double result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else if (op == '/') { if (num2 != 0) { result = num1 / num2; } else { System.out.println("Error: Division by zero"); scanner.close(); return; } } else { System.out.println("Invalid operator"); scanner.close(); return; } System.out.println("Result: " + result); scanner.close(); } }
If-else is more flexible for complex conditions but can be longer than switch.
Using methods for each operation
java
import java.util.Scanner; public class CalculatorWithMethods { public static double add(double a, double b) { return a + b; } public static double subtract(double a, double b) { return a - b; } public static double multiply(double a, double b) { return a * b; } public static double divide(double a, double b) { if (b == 0) throw new IllegalArgumentException("Division by zero"); return a / b; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); System.out.print("Enter operator (+, -, *, /): "); char op = scanner.next().charAt(0); double result; try { switch (op) { case '+': result = add(num1, num2); break; case '-': result = subtract(num1, num2); break; case '*': result = multiply(num1, num2); break; case '/': result = divide(num1, num2); break; default: System.out.println("Invalid operator"); scanner.close(); return; } System.out.println("Result: " + result); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } scanner.close(); } }
Using methods improves code organization and reusability but adds more lines.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables for input and result, so space usage is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; using methods adds slight overhead but improves readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple and clear operation selection |
| If-else statements | O(1) | O(1) | Flexible conditions, longer code |
| Separate methods | O(1) | O(1) | Better organization and reusability |
Always check for division by zero to avoid runtime errors.
Beginners often forget to handle division by zero, causing the program to crash.