Skip to content

Commit e97cdb2

Browse files
authored
Create Calculator.java
1 parent ddc8a74 commit e97cdb2

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Calculator.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.Scanner;
2+
3+
public class Calculator {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner input = new Scanner(System.in);
8+
9+
int ans = 0;
10+
11+
while (true) {
12+
13+
System.out.print("Enter a operator [+ - * / %] = ");
14+
15+
char op = input.next().trim().charAt(0);
16+
17+
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
18+
19+
System.out.print("Enter one numbers : ");
20+
21+
int num1 = input.nextInt();
22+
23+
System.out.print("Enter two numbers : ");
24+
25+
int num2 = input.nextInt();
26+
27+
if (op == '+') {
28+
29+
ans = num1 + num2;
30+
31+
}
32+
33+
if (op == '-') {
34+
35+
ans = num1 - num2;
36+
37+
}
38+
39+
if (op == '*') {
40+
41+
ans = num1 * num2;
42+
43+
}
44+
45+
if (op == '/') {
46+
47+
if (num2 != 0) {
48+
49+
ans = num1 / num2;
50+
51+
}
52+
53+
}
54+
55+
if (op == '%') {
56+
57+
ans = num1 % num2;
58+
59+
}
60+
61+
} else if (op == 'x' || op == 'X') {
62+
63+
break;
64+
65+
}
66+
67+
System.out.println("Result = " + ans);
68+
69+
}
70+
71+
}
72+
73+
}

0 commit comments

Comments
 (0)