-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalculator.java
162 lines (132 loc) · 3.67 KB
/
Calculator.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.Scanner;
// Calculator
// A Paul Njuguna & Patience Nyarko creation
public class Calculator{
//ADDITION
public static void add(double num1,double num2){
double result=num1+num2;
System.out.println("The sum is: "+result);
}
//SUBTRACTION
public static void subtract(double num1,double num2){
double result=num1-num2;
System.out.println("The subtration is: "+result);
}
//PRODUCT
public static void product(double num1,double num2){
double result=num1*num2;
System.out.println("The product is: "+result);
}
//DIVISION
public static void division(double num1,double num2){
double result=num1/num2;
System.out.println("The division is: "+result);
}
//MODULO
public static void mod(int num1,int num2){
int result=num1%num2;
System.out.println(num1+" modulo "+num2+" is "+result);
}
//POWER
public static void power(int num1,int num2){
double result=1;
for (int i=1;i<=num2;i++ ) {
result=result*num1;
}
System.out.println(num1+" raised to power of "+num2+" is "+result);
}
//SQUARE ROOT
public static void squareRt(double num1){
double i;
double result = num1 / 2;
do{
i = result;
result = (i + (num1 / i)) / 2;
} while ((i - result) != 0);
System.out.println("The square root of "+num1+" is "+result);
}
//AVERAGE
public static void average(int num1,int num2){
float result=(float)num1+(((float)num2-(float)num1)/2);
System.out.println("The average is: "+result);
}
public static void main(String[] args) {
int option;
Scanner in= new Scanner(System.in);
System.out.println("CHOOSE AN OPTION");
System.out.println("-------------------");
System.out.println("1- Modulo");
System.out.println("2- Power");
System.out.println("3- Square root");
System.out.println("4- Sum");
System.out.println("5- Subtraction");
System.out.println("6- Product");
System.out.println("7- Division");
System.out.println("8- average");
System.out.println("-------------------");
System.out.println("Enter choice(1-8):");
option = Integer.parseInt(in.nextLine());
if (option==1) {
int f1,f2;
System.out.println("Enter the first number:");
f1 = Integer.parseInt(in.nextLine());
System.out.println("Enter the second number:");
f2 = Integer.parseInt(in.nextLine());
mod(f1,f2);
}
else if (option==2) {
int f1,f2;
System.out.println("Enter the base number:");
f1 = Integer.parseInt(in.nextLine());
System.out.println("Enter the exponent number:");
f2 = Integer.parseInt(in.nextLine());
power(f1,f2);
}
else if (option==3) {
double f1;
System.out.println("Enter number:");
f1 = in.nextDouble();
squareRt(f1);
}
else if (option==4) {
double f1,f2;
System.out.println("Enter the first number:");
f1 = in.nextDouble();
System.out.println("Enter the second number:");
f2 = in.nextDouble();
add(f1,f2);
}
else if (option==5) {
double f1,f2;
System.out.println("Enter the first number:");
f1 = in.nextDouble();
System.out.println("Enter the second number:");
f2 = in.nextDouble();
subtract(f1,f2);
}
else if (option==6) {
double f1,f2;
System.out.println("Enter the first number:");
f1 = in.nextDouble();
System.out.println("Enter the second number:");
f2 = in.nextDouble();
product(f1,f2);
}
else if (option==7) {
double f1,f2;
System.out.println("Enter the first number:");
f1 = in.nextDouble();
System.out.println("Enter the second number:");
f2 = in.nextDouble();
division(f1,f2);
}
else if (option==8) {
int f1,f2;
System.out.println("Enter the first number:");
f1 = in.nextInt();
System.out.println("Enter the second number:");
f2 = in.nextInt();
average(f1,f2);
}
}
}