Introduction
In this guide, we will develop a simple calculator program in Java that can perform basic arithmetic operations like addition, subtraction, multiplication, and division. The program will take input from the user, process the input, and display the result of the operation.
Problem Statement
The task is to create a calculator that can handle the following operations:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
)
The program should prompt the user to input two numbers and an operator, then perform the corresponding operation and display the result.
Example 1:
- Input:
5
,3
,+
- Output:
8.0
Example 2:
- Input:
10
,2
,/
- Output:
5.0
Example 3:
- Input:
7
,3
,*
- Output:
21.0
Solution Steps
Prompt the User for Input: Ask the user to enter two numbers and an operator.
Perform the Operation: Use a
switch
statement to determine which operation to perform based on the operator provided by the user.Display the Result: After performing the operation, display the result to the user.
Java Program
import java.util.Scanner;
/**
* Simple Calculator Program in Java
* Author: https://www.javaguides.net/
*/
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
// Perform the operation
switch (operator) {
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.");
return;
}
break;
default:
System.out.println("Invalid operator! Please use +, -, *, or /.");
return;
}
// Display the result
System.out.println("The result is: " + result);
}
}
Step by Step Explanation
User Input: The program begins by prompting the user to input two numbers (
num1
andnum2
) and an operator (+
,-
,*
, or/
).Operation:
- The
switch
statement is used to determine which operation to perform based on the operator. - The program checks the operator:
- If it’s
+
, the program addsnum1
andnum2
. - If it’s
-
, the program subtractsnum2
fromnum1
. - If it’s
*
, the program multipliesnum1
bynum2
. - If it’s
/
, the program dividesnum1
bynum2
but checks ifnum2
is zero to avoid division by zero.
- If it’s
- If the operator is not recognized, the program displays an error message and exits.
- The
Result Display: The program prints the result of the operation to the user.
Output Examples
Example 1: Addition
Enter first number: 5
Enter second number: 3
Enter an operator (+, -, *, /): +
The result is: 8.0
Example 2: Division
Enter first number: 10
Enter second number: 2
Enter an operator (+, -, *, /): /
The result is: 5.0
Example 3: Multiplication
Enter first number: 7
Enter second number: 3
Enter an operator (+, -, *, /): *
The result is: 21.0
Example 4: Division by Zero
Enter first number: 10
Enter second number: 0
Enter an operator (+, -, *, /): /
Error! Division by zero.
Conclusion
This simple calculator program demonstrates how to take user input in Java and perform basic arithmetic operations. By using a switch
statement, the program can handle different operators and provide the corresponding result. This program is a good starting point for beginners to learn about user input, control flow, and basic arithmetic in Java.
Comments
Post a Comment
Leave Comment