-
Notifications
You must be signed in to change notification settings - Fork 37
/
BasicCalculator.java
31 lines (31 loc) · 1.01 KB
/
BasicCalculator.java
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
public class BasicCalculator {
public static void main(String[] args) {
try(Scanner sc = new Scanner(System.in)){
System.out.println("Enter the first number:");
int n1 = sc.nextInt();
System.out.println("Enter the second number:");
int n2 = sc.nextInt();
System.out.println("Enter the operator:");
char op = sc.next().charAt(0);
int result = 0;
switch(op){
case '+':
result = n1 + n2;
break;
case '-':
result = n1 - n2;
break;
case '*':
result = n1 * n2;
break;
case '/':
result = n1 / n2;
break;
default:
System.out.println("Invalid operator");
break;
}
System.out.println("The result is " + result);
}
}
}