forked from Swatigupta-droid/Basic-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.java
More file actions
59 lines (55 loc) · 2.04 KB
/
calculator.java
File metadata and controls
59 lines (55 loc) · 2.04 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
// package Basic-java;
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Take input from user untill user doesn't press X or x.
while(true) {
// Take operator as input.
System.out.print("Enter The operator: ");
char operator = in.next().trim().charAt(0);
int result = 0;
if(operator == '+' || operator == '-' || operator == '*' || operator == '/' || operator == '%') {
// Input of Two num.
System.out.print("Enter Two no. for operation: ");
int num1 = in.nextInt();
int num2 = in.nextInt();
// Addition
if(operator == '+') {
result = num1 + num2;
}
if(operator == '-') {
result = num1 - num2;
}
if(operator == '*') {
result = num1 * num2;
}
if(operator == '/') {
if(num2 > num1){
if(num1 != 0){
result = num2 / num1;
} else {
System.out.println("Undefine Result due to num1");
}
} else {
if(num2 != 0){
result = num1 / num2;
} else {
System.out.println("Undefine Result due to num2");
}
}
}
if(operator == '%') {
result = num1 % num2;
}
System.out.println(result);
} else if(operator == 'X' || operator == 'x') {
System.out.println("Program terminated Successfully! :-)");
break;
} else {
System.out.println("Invalid Opertion! :~)");
return;
}
}
}
}