This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
44 lines (44 loc) · 1.6 KB
/
SimpleCalculator.java
File metadata and controls
44 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package HarshTamboli.SimpleCalculator;
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("Enter the first number:");
double num1 = scanner.nextDouble();
System.out.println("Enter the second number:");
double num2 = scanner.nextDouble();
System.out.println("Select operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
int choice = scanner.nextInt();
double result;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("Result: " + result);
break;
case 2:
result = num1 - num2;
System.out.println("Result: " + result);
break;
case 3:
result = num1 * num2;
System.out.println("Result: " + result);
break;
case 4:
if (num2 == 0) {
System.out.println("Error: Division by zero");
} else {
result = num1 / num2;
System.out.println("Result: " + result);
}
break;
default:
System.out.println("Invalid choice");
}
scanner.close();
}
}