-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator in Java.txt
49 lines (43 loc) · 1.53 KB
/
Calculator in Java.txt
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
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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 choice = sc.nextInt();
System.out.print("Enter the first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter the second number: ");
double num2 = sc.nextDouble();
double result;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("The sum is: " + result);
break;
case 2:
result = num1 - num2;
System.out.println("The difference is: " + result);
break;
case 3:
result = num1 * num2;
System.out.println("The product is: " + result);
break;
case 4:
if (num2 == 0) {
System.out.println("Cannot divide by zero");
} else {
result = num1 / num2;
System.out.println("The quotient is: " + result);
}
break;
default:
System.out.println("Invalid choice");
break;
}
sc.close();
}
}