Simple Calculator in Java Using If-Else and Switch-Case
Java Project for Beginners - Simple Calculator:
Simple Calculator:
Perform basic arithmetic operations like addition, subtraction, multiplication, and division.
The Simple Calculator project allows users to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The user inputs two numbers and selects an operation, and the calculator processes the input to return the result. It handles basic mathematical calculations and can be extended to include additional features like error handling (e.g., division by zero) or supporting more advanced operations like exponentiation or modulus.
Input: Two numbers and an arithmetic operation (+, -, *, /).
Output: The result of the operation.
Example:
- Input: 5, 3, +
- Output: 8
Here are two different solutions for the Simple Calculator project in Java.
Solution 1: Using If-Else Statements
Code:
import java.util.Scanner;
public class SimpleCalculatorIfElse {
public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);
// Input first number
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Input second number
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Input the operator (+, -, *, /)
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
// Variable to store the result
double result = 0;
// Perform the operation based on the user input using if-else
if (operator == '+') {
result = num1 + num2;
} else if (operator == '-') {
result = num1 - num2;
} else if (operator == '*') {
result = num1 * num2;
} else if (operator == '/') {
// Handle division by zero case
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error! Division by zero.");
return;
}
} else {
// If an invalid operator is entered
System.out.println("Invalid operator! Please enter +, -, *, or /.");
return;
}
// Output the result
System.out.println("The result is: " + result);
}
}
Output:
Enter the first number: 12 Enter the second number: 33 Enter an operator (+, -, *, /): * The result is: 396.0
Enter the first number: 12 Enter the second number: 0 Enter an operator (+, -, *, /): / Error! Division by zero.
Explanation :
- Input: The program prompts the user to input two numbers and an operator.
- Operations: The arithmetic operations are handled using if-else conditions based on the operator entered.
- Edge Case: Division by zero is checked to prevent errors.
- Result: The result is displayed to the user.
- Invalid Input: If an operator other than +, -, *, or / is entered, the program notifies the user.
Solution 2: Using Switch-Case Statements
Code:
import java.util.Scanner;
public class SimpleCalculatorSwitchCase {
public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);
// Input first number
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Input second number
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Input the operator (+, -, *, /)
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
// Variable to store the result
double result = 0;
// Perform the operation using switch-case
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
// Handle division by zero
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error! Division by zero.");
return;
}
break;
default:
// If an invalid operator is entered
System.out.println("Invalid operator! Please enter +, -, *, or /.");
return;
}
// Output the result
System.out.println("The result is: " + result);
}
}
Output:
Enter the first number: 100 Enter the second number: 0 Enter an operator (+, -, *, /): / Error! Division by zero.
Enter the first number: 12 Enter the second number: 100 Enter an operator (+, -, *, /): - The result is: -88.0
Explanation:
- Input: The user inputs two numbers and an operator.
- Switch-Case: The operations are performed using a switch-case block based on the operator entered.
- Edge Case: Division by zero is handled inside the switch block to avoid errors.
- Result: The result is displayed after performing the calculation.
- Invalid Input: The default case of the switch block handles invalid operators and informs the user.
Java Code Editor:
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/java/java-project-basic-arithmetic-calculator.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics