-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.java
More file actions
54 lines (43 loc) · 1.61 KB
/
calculator.java
File metadata and controls
54 lines (43 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
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 (/)");
System.out.print("Enter your choice (1-4): ");
int choice = sc.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) {
result = num1 / num2;
System.out.println("Result = " + result);
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid choice! Please enter 1-4.");
}
sc.close();
}
}