Skip to content

Commit 78b0787

Browse files
committed
If-Else Practice Added.
Signed-off-by: Someshdiwan <[email protected]>
1 parent dfe5bfe commit 78b0787

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Scanner;
2+
3+
public class Calculator {
4+
public static void main(String[] args) {
5+
Scanner in = new Scanner(System.in);
6+
// Take input from user till user does not press X or x
7+
int ans = 0;
8+
while (true) {
9+
// take the operator as input
10+
System.out.print("Enter the operator: ");
11+
char op = in.next().trim().charAt(0);
12+
13+
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
14+
// input two numbers
15+
System.out.print("Enter two numbers: ");
16+
int num1 = in.nextInt();
17+
int num2 = in.nextInt();
18+
19+
if (op == '+') {
20+
ans = num1 + num2;
21+
}
22+
if (op == '-') {
23+
ans = num1 - num2;
24+
}
25+
if (op == '*') {
26+
ans = num1 * num2;
27+
}
28+
if (op == '/') {
29+
if (num2 != 0) {
30+
ans = num1 / num2;
31+
}
32+
}
33+
if (op == '%') {
34+
ans = num1 % num2;
35+
}
36+
} else if (op == 'x' || op == 'X') {
37+
break;
38+
} else {
39+
System.out.println("Invalid operation!!");
40+
}
41+
System.out.println(ans);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)