-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
46 lines (38 loc) · 1.25 KB
/
Copy pathCalculator.java
File metadata and controls
46 lines (38 loc) · 1.25 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
import java.io.*;
import java.util.Scanner;
public class Calculator{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number:");
double num1 = sc.nextDouble();
System.out.println("Enter second number:");
double num2 = sc.nextDouble();
System.out.println("Choose operation: + - * /");
char op = sc.next().charAt(0);
double result;
if (op == '+') {
result = num1 + num2;
System.out.println("Result: " + result);
}
else if (op == '-') {
result = num1 - num2;
System.out.println("Result: " + result);
}
else if (op == '*') {
result = num1 * num2;
System.out.println("Result: " + result);
}
else if (op == '/') {
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero");
}
}
else {
System.out.println("Invalid operation");
}
sc.close();
}
}