-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculatorUsingSwitchCase.java
More file actions
56 lines (36 loc) · 1.55 KB
/
SimpleCalculatorUsingSwitchCase.java
File metadata and controls
56 lines (36 loc) · 1.55 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
/*Question : Make a simple claculator using Switch-Case */
import java.util.Scanner; //import Scanner library
public class SimpleCalculatorUsingSwitchCase{
public static void main(String[] args){
Scanner n=new Scanner(System.in);
float a,b,r;
char c; // declair char(charecter) variable to take operator
//Take 1st input from the user
System.out.print("Enter the 1st number : ");
a=n.nextFloat();
// Take the operator from the user
System.out.print("Choose the operator (+\\-\\*\\/): ");
c=n.next().charAt(0);
//Take 2nd input from the user
System.out.print("Enter the 2nd number : ");
b=n.nextFloat();
switch(c){
case '+':
r=a+b;
System.out.println("After performing "+a+" + "+b+" we get "+ r );
break;
case '-':
r=a-b;
System.out.println("After performing "+a+" - "+b+" we get "+ r );
break;
case '*':
r=a*b;
System.out.println("After performing "+a+" * "+b+" we get "+ r );
break;
case '/':
r=a/b;
System.out.println("After performing "+a+" / "+b+" we get "+ r );
break;
}
}
}