-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab2A.java
More file actions
61 lines (55 loc) · 1.35 KB
/
Lab2A.java
File metadata and controls
61 lines (55 loc) · 1.35 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
55
56
57
58
59
60
61
// 2A: Write a java program to develop a simple calculator using switch statement.
import java.util.Scanner;
class Lab2A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b;
float c = 0;
System.out.println("Enter 2 numbers: ");
a = s.nextInt();
b = s.nextInt();
System.out.println("Enter the Operator: ");
char op = s.next().charAt(0);
switch (op) {
case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
c = a * b;
break;
case '/':
if (a == 0 || b == 0) {
System.out.println("ZeroDivisionError");
break;
} else {
c = (float) a / b;
break;
}
default:
System.out.println("Invalid Input");
break;
}
System.out.println("Result of " + a + op + b + "=" + c);
}
}
/*
* Output:
* Enter 2 numbers:
* 5 6
* Enter the Operator:
* +
* Result of 5+6=11.0
* Enter 2 numbers:
* 5 6
* Enter the Operator:
* /
* Result of 5/6=0.8333333
* Enter 2 numbers:
* 5 0
* Enter the Operator:
* /
* ZeroDivisionError
*/