-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorApp.java
More file actions
79 lines (61 loc) · 1.54 KB
/
CalculatorApp.java
File metadata and controls
79 lines (61 loc) · 1.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
class Calculator {
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a*b;
}
int divide(int a, int b){
return a/b;
}
int modulus(int a, int b) {
return a%b;
}
}
class CalculatorApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Calculator calc = new Calculator(); //object creation
System.out.println("MENU");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println("5.Modulus");
System.out.println("6.Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.println("Enter first number: ");
int a = sc.nextInt();
System.out.println("Enter second number: ");
int b = sc.nextInt();
if(choice == 1) {
System.out.println("Result: "+ calc.add(a,b));
}
else if(choice == 2) {
System.out.print("Result: "+calc.subtract(a,b));
}
else if(choice == 3) {
System.out.print("Result: "+ calc.multiply(a,b));
}
else if(choice == 4) {
if(b!=0){
System.out.println("Result: " + calc.divide(a,b));
}
else {
System.out.println("cannot divide with 0");
}
}
else if(choice == 5) {
System.out.println("Result: " +calc.modulus(a,b));
}
else {
System.out.println("Invalid choice!");
}
sc.close();
}
}