Algorithm:
- Declare two variables for the operands and one variable for the operator.
- Read the operands and operator from the user.
- Use a switch statement to perform the appropriate arithmetic operation based on the operator entered by the user.
- Display the result of the operation to the user.
This implementation prompts the user to enter two numbers and an operator, then uses a switch statement to perform the appropriate arithmetic operation and display the result. Note that this implementation assumes that the user enters valid input. In a more robust implementation, you would need to add error handling to ensure that the user enters valid input.
Code:
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int c;
double n1, n2;
System.out.println("1. Add \n 2. Subtract \n 3. Multiply \n 4. Divide");
c = scanner.nextInt();
System.out.print("Enter First Number : ");
n1 = scanner.nextDouble();
System.out.print("Enter Second Number : ");
n2 = scanner.nextDouble();
double result = 0;
switch(c) {
case 1:
result = n1 + n2;
break;
case 2:
result = n1 - n2;
break;
case 3:
result = n1 * n2;
break;
case 4:
result = n1 / n2;
break;
default:
System.out.println("Invalid Input");
break;
}
System.out.println("Result : "+result);
}
}
Output:
1. Add
2. Subtract
3. Multiply
4. Divide
1
Enter First Number : 10
Enter Second Number : 20
Result : 30.0
Explanation:
This is a simple Java program that performs basic arithmetic operations based on user input. Here's a breakdown of what the code does:
- The program starts by importing the Scanner class, which allows the program to read user input from the command line.
- The public class calculator defines the class name for the program.
- The main method is the entry point for the program. It first creates a new Scanner object to read input from the user.
- It then declares three variables: c, n1, and n2. c is an integer that stores the user's choice of arithmetic operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division). n1 and n2 are doubles that store the two numbers to be used in the arithmetic operation.
- The program then prompts the user to choose an operation by printing a menu of options. It uses the nextInt() method of the Scanner object to read the user's input and stores it in the c variable.
- The program then prompts the user to enter two numbers, using the nextDouble() method of the Scanner object to read each number and store it in the n1 and n2 variables.
- The program then declares a variable result and sets it to 0.
- The program then uses a switch statement to perform the arithmetic operation based on the user's choice of operation. If the user entered an invalid choice, the default case prints an error message.
- After performing the arithmetic operation, the program prints the result using the println() method, concatenating the result with the string "Result : ".
- The program then ends.
Overall, this program is a simple demonstration of how to use a switch statement in Java to perform different operations based on user input.
Related Links:
- GUI for Calculator
- QR Code Generator
- Create a function that takes two arguments: the original price and the discount percentage as integers and returns the final price after the discount.
- Selection Sort
- Print the sum of series using C programming
- Check whether a person can Drive or not using Ternary Operator
- Top 50 OOPS (Object Oriented Programming System) Interview Questions
- Animated Login page using HTML and CSS
- Write a program in Java to accept a name(Containing three words) and Display only the initials (i.e., first letter of each word).
- How can I get better at JavaScript?
- How to create a age calculator using tkinter in Python?
0 Comments
Ask Your Queries in the comments