Let us create a simple Calculator project in java. It has four options. Addition, Subtraction, Multiplication and Division.
Steps:
- Get the input from user for the type of arithmetic operation as integer(1,2,3 or 4).
- Get the two inputs from the user.
- Using switch case, choose the case according to the user input.
- Process the statement and print the output.
Program:
import
java.util.Scanner;
public class CalculatorApp {
public static
void main(String[] args) {
Scanner
scanner1 = new Scanner(System.in);
System.out.println("Simple Calculator Application");
System.out.println("Choose an operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
int choice1
= scanner1.nextInt();
System.out.print("Enter the first number: ");
double no1
= scanner1.nextDouble();
System.out.print("Enter the second number: ");
double no2
= scanner1.nextDouble();
double res
= 0;
switch
(choice1) {
case 1:
res
= no1 + no2;
System.out.println("Addition of two numbers: " + res);
break;
case 2:
res
= no1 - no2;
System.out.println("Subtraction of two numbers: " + res);
break;
case 3:
res
= no1 * no2;
System.out.println("Multiplication of two numbers: " + res);
break;
case 4:
if
(no2 != 0) {
res = no1 / no2;
System.out.println("Division of two numbers: " + res);
}
else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid choice. Please select a valid
operation.");
}
scanner1.close();
}
}
C:\raji\blog>javac CalculatorApp.java
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter the first number: 56
Enter the second number: 67
Addition of two numbers: 123.0
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
2
Enter the first number: 78
Enter the second number: 45
Subtraction of two numbers: 33.0
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
3
Enter the first number: 34
Enter the second number: 5
Multiplication of two numbers: 170.0
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter the first number: 67
Enter the second number: 4
Division of two numbers: 16.75
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5
Enter the first number: 67
Enter the second number: 78
Invalid choice. Please select a valid operation.
C:\raji\blog>java CalculatorApp
Simple Calculator Application
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter the first number: 69
Enter the second number:
0
Error: Division by zero is not allowed.
Thus the simple Calculator project is written and executed
successfully. Keep coding!!!!
No comments:
Post a Comment