-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
28 lines (21 loc) · 789 Bytes
/
Calculator.java
File metadata and controls
28 lines (21 loc) · 789 Bytes
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
import java.util.*;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter second number: ");
double b = sc.nextDouble();
System.out.print("Enter operator (+ - * /): ");
char op = sc.next().charAt(0);
double result = 0;
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = b != 0 ? a / b : 0; break;
default: System.out.println("Invalid operator!");
}
System.out.println("Result = " + result);
}
}