-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalc.java
More file actions
429 lines (428 loc) · 21.4 KB
/
Copy pathcalc.java
File metadata and controls
429 lines (428 loc) · 21.4 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import java.util.*;
public class calc {
public static void main(String[] args){
double a, b, c = 0, e = 0, x, y, z;
int d, numS;
Scanner calc = new Scanner(System.in);
System.out.print("\033[H\033[2J");
while(e == 0){
System.out.println("\n1. Addition + \n2. Subtraction - \n3. Multipication * \n4. Division / \n5. Modulus % \n6. Hypotenuse \n7. Power of a Number \n8. Random Number \n9. Converter \n10. Armstrong Number \n11. Reverse a Number \n12. Circle \n13. Rectangle \n14. Tables \n15. Matrices \n16. Exit \nEnter Your Choice: \n");
d = calc.nextInt();
switch(d){
case 1:
System.out.print("\033[H\033[2J");
System.out.println("\nAddition\n");
System.out.println("Enter How many Numbers you want to SUM: \n");
numS = calc.nextInt();
System.out.println("Enter the " + numS + " Numbers: \n");
for(int i = 1; i <= numS; i++){
System.out.println("Enter Number " + (i) + " : \n");
a = calc.nextDouble();
c += a;
}
System.out.println("\nThe Sum of " + numS + " Numbers is " + c );
break;
case 2:
System.out.print("\033[H\033[2J");
System.out.println("\nSubtraction\n");
System.out.println("Enter How many Numbers you want to Substract: \n");
numS = calc.nextInt();
System.out.println("Enter Number 1: \n");
c = calc.nextDouble();
System.out.println("Enter the " + numS + " Numbers: \n");
for(int i = 2; i <= numS; i++){
System.out.println("Enter Number " + (i) + " : \n");
a = calc.nextDouble();
c -= a;
}
System.out.println("\nThe Difference of " + numS + " Numbers is " + c );
break;
case 3:
System.out.print("\033[H\033[2J");
System.out.println("\nMultipication\n");
System.out.println("Enter First Number: \n");
a = calc.nextDouble();
System.out.println("Enter Second Number: \n");
b = calc.nextDouble();
c = a * b;
System.out.println("\nThe Combination of " + a + " and " + b + " is " + c );
break;
case 4:
System.out.print("\033[H\033[2J");
System.out.println("\nDivision\n");
System.out.println("Enter First Number: \n");
a = calc.nextDouble();
System.out.println("Enter Second Number: \n");
b = calc.nextDouble();
c = a / b;
System.out.println("\nThe Divison of " + a + " and " + b + " is " + c );
break;
case 5:
System.out.print("\033[H\033[2J");
System.out.println("\nModulus\n");
System.out.println("Enter First Number: \n");
a = calc.nextDouble();
System.out.println("Enter Second Number: \n");
b = calc.nextDouble();
c = a % b;
System.out.println("\nThe Modulus of " + a + " and " + b + " is " + c );
break;
case 6:
System.out.print("\033[H\033[2J");
System.out.println("Enter side x: ");
x = calc.nextDouble();
System.out.println("\nEnter side y: ");
y = calc.nextDouble();
z = Math.sqrt((x * x) + (y * y));
System.out.println("The Hypotenuse is " + z);
break;
case 7:
System.out.print("\033[H\033[2J");
Scanner pow = new Scanner(System.in);
int base, exp, oexp, result = 1;
System.out.println("Enter Base Number: ");
base = pow.nextInt();
System.out.println("Enter an Exponent: ");
exp = pow.nextInt();
oexp = exp;
while(exp != 0){
result *= base;
--exp;
}
System.out.println("The Power of " + base + " Raised to " + oexp + " is " + result);
break;
case 8:
System.out.print("\033[H\033[2J");
int min, max;
Scanner randN = new Scanner(System.in);
System.out.println("Enter the min number: ");
min = randN.nextInt();
System.out.println("Enter the max number: ");
max = randN.nextInt();
int randNumber = (int)(Math.random() * (max - min + 1) + min);
System.out.println("Random Number Between " + min + " to " + max + " is " + randNumber);
break;
case 9:
System.out.print("\033[H\033[2J");
double f, cel;
int option;
Scanner con = new Scanner(System.in);
System.out.println("1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius \nEnter Your Choice: ");
option = con.nextInt();
switch(option){
case 1:
System.out.println("Enter Temperature in Celsius: ");
cel = con.nextDouble();
f = (cel * 9/5) + 32;
System.out.println("The Temperature of " + cel + " Celsius in Fahrenheit is: " + f);
break;
case 2:
System.out.println("Enter Temperature in Fahrenheit: ");
f = con.nextDouble();
cel = (f - 32) * 5/9;
System.out.println("The Temperature of " + f + " Fahrenheit in Celsius is: " + cel);
break;
}
break;
case 10:
System.out.print("\033[H\033[2J");
int ce = 0, ae, temp, n;
Scanner num = new Scanner(System.in);
System.out.print("Enter number to be checked: ");
n = num.nextInt();
temp = n;
while(n > 0){
ae = n % 10;
n = n/10;
ce = ce+(ae*ae*ae);
}
if(temp == ce){
System.out.println(temp+" is an Armstrong Number.");
}else{
System.out.println(temp+" is not an Armstrong Number.");
}
break;
case 11:
System.out.print("\033[H\033[2J");
Scanner revNum = new Scanner(System.in);
int rnum, rev = 0, remainder;
System.out.println("Enter an Integer: ");
rnum = revNum.nextInt();
while(rnum != 0){
remainder = rnum % 10;
rev = rev * 10 + remainder;
rnum /= 10;
}
System.out.println("Reversed Number of " + rnum + " is " + rev);
break;
case 12:
System.out.print("\033[H\033[2J");
Scanner circle = new Scanner(System.in);
double radius, circumference, area, diameter;
int choice;
System.out.println("Circle\n1. Radius \n2. Diameter \n3. Circumference \n4. Area \nEnter Your Choice: ");
choice = circle.nextInt();
switch(choice){
case 1:
System.out.println("Enter Circumference of Circle: ");
circumference = circle.nextDouble();
radius = circumference/(2 * 3.14);
System.out.println("Radius of Circumference " + circumference + " = " + radius);
break;
case 2:
System.out.println("Enter Radius of Circle: ");
radius = circle.nextDouble();
diameter = 2 * radius;
System.out.println("The Diameter of radius " + radius + " Circle = " + diameter);
break;
case 3:
System.out.println("Enter Radius of Circle: ");
radius = circle.nextDouble();
circumference = 2 * 3.14 * radius;
System.out.println("The Circumference of Radius " + radius + " Circle = " + circumference);
break;
case 4:
System.out.println("Enter Radius of Circle: ");
radius = circle.nextDouble();
area = 3.14 * radius * radius;
System.out.println("The Area of Radius " + radius + " Circle = " + area);
break;
}
break;
case 13:
System.out.print("\033[H\033[2J");
double length, width, rectangleArea, perimeter, diagonal;
int option1;
Scanner rectangle = new Scanner(System.in);
System.out.println("Rectangle\n1. Area \n2. Diagonal \n3. Perimeter \n4. Length \n5. Width \nEnter Your Choice: ");
option1 = rectangle.nextInt();
switch(option1){
case 1:
System.out.println("Enter Length of the Rectangle: ");
length = rectangle.nextDouble();
System.out.println("Enter Width of the Rectangle: ");
width = rectangle.nextDouble();
rectangleArea = length * width;
System.out.println("The Area of the Rectangle " + length + " Length " + width + " Width is " + rectangleArea);
break;
case 2:
System.out.println("Enter Length of the Rectangle: ");
length = rectangle.nextDouble();
System.out.println("Enter Width of the Rectangle: ");
width = rectangle.nextDouble();
diagonal = Math.sqrt((length * length) + (width * width));
System.out.println("The Diagonal of the Rectangle " + length + " Length " + width + " width is " + diagonal);
break;
case 3:
System.out.println("Enter Length of the Rectangle: ");
length = rectangle.nextDouble();
System.out.println("Enter Width of the Rectangle: ");
width = rectangle.nextDouble();
perimeter = 2 * (length + width);
System.out.println("The Perimeter of the Rectangle " + length + " Length " + width + " width is " + perimeter);
break;
case 4:
System.out.println("Enter Width of the Rectangle: ");
width = rectangle.nextDouble();
System.out.println("Enter Perimeter of the Rectangle: ");
perimeter = rectangle.nextDouble();
length = (perimeter/2) - width;
System.out.println("The Length of Rectangle whose Perimeter = " + perimeter + " and Width = " + width + " is " + length);
break;
case 5:
System.out.println("Enter Length of the Rectangle: ");
length = rectangle.nextDouble();
System.out.println("Enter Perimeter of the Rectangle: ");
perimeter = rectangle.nextDouble();
width = (perimeter/2) - length;
System.out.println("The Width of Rectangle whose Perimeter = " + perimeter + " and Length = " + length + " is " + width);
break;
}
break;
case 14:
System.out.print("\033[H\033[2J");
Scanner tables = new Scanner(System.in);
int tNum;
System.out.println("Enter a Number Whose Table you want to see: ");
tNum = tables.nextInt();
System.out.println("\n" + tNum + " x 1 = " + tNum * 1 + " \n");
System.out.println(tNum + " x 2 = " + tNum * 2 + " \n");
System.out.println(tNum + " x 3 = " + tNum * 3 + " \n");
System.out.println(tNum + " x 4 = " + tNum * 4 + " \n");
System.out.println(tNum + " x 5 = " + tNum * 5 + " \n");
System.out.println(tNum + " x 6 = " + tNum * 6 + " \n");
System.out.println(tNum + " x 7 = " + tNum * 7 + " \n");
System.out.println(tNum + " x 8 = " + tNum * 8 + " \n");
System.out.println(tNum + " x 9 = " + tNum * 9 + " \n");
System.out.println(tNum + " x 10 = " + tNum * 10 + " \n");
break;
case 15:
System.out.print("\033[H\033[2J");
int rows, rows1, columns, columns1, option2, i, j, k, rSum = 0;
int[][] matrix1 = new int[100][100], matrix2 = new int[100][100], mSum = new int[100][100];
Scanner matrix = new Scanner(System.in);
System.out.println("1. Addition \n2. Subtraction \n3. Multipication \n4. Transpose \nEnter Your Choice: ");
option2 = matrix.nextInt();
switch(option2){
case 1:
System.out.println("Enter Number of Rows: ");
rows = matrix.nextInt();
System.out.println("Enter Number of Columns: ");
columns = matrix.nextInt();
System.out.println("\nEnter elements of 1st matrix: \n");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element A%d%d: ", i + 1, j + 1);
matrix1[i][j] = matrix.nextInt();
}
}
System.out.println("Enter element of 2nd matrix: ");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element B%d%d: ", i + 1, j + 1);
matrix2[i][j] = matrix.nextInt();
}
}
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
mSum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("\nSum of Two Matrices: \n");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("%d ", mSum[i][j]);
if(j == columns - 1){
System.out.println("\n");
}
}
}
break;
case 2:
System.out.println("Enter Number of Rows: ");
rows = matrix.nextInt();
System.out.println("Enter Number of Columns: ");
columns = matrix.nextInt();
System.out.println("\nEnter elements of 1st matrix: \n");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element A%d%d: ", i + 1, j + 1);
matrix1[i][j] = matrix.nextInt();
}
}
System.out.println("Enter element of 2nd matrix: ");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element B%d%d: ", i + 1, j + 1);
matrix2[i][j] = matrix.nextInt();
}
}
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
mSum[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
System.out.println("\nDifference of Two Matrices: \n");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("%d ", mSum[i][j]);
if(j == columns - 1){
System.out.println("\n");
}
}
}
break;
case 3:
System.out.println("Enter the number of rows of Matrix 1: ");
rows = matrix.nextInt();
System.out.println("Enter the number of columns of Matrix 1: ");
columns = matrix.nextInt();
matrix1 = new int[rows][columns];
System.out.println("Enter elements of Matrix 1: ");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element A%d%d: ", i + 1, j + 1);
matrix1[i][j] = matrix.nextInt();
}
}
System.out.println("Enter the number of rows of Matrix 2: ");
rows1 = matrix.nextInt();
System.out.println("Enter the number of columns of Matrix 2: ");
columns1 = matrix.nextInt();
if(columns != rows1){
System.out.println("The Matrices cant be multiplied with each other as Number of columns of Matrix 1 id not equal to Number of rows of Matrix 2.");
}else{
int[][] second = new int[rows1][columns1];
int[][] multiply = new int[rows][columns1];
System.out.println("Enter Elements of Matrix 2: ");
for(i = 0; i < rows1; ++i){
for(j = 0; j < columns1; ++j){
System.out.format("Enter Element A%d%d: ", i + 1, j + 1);
second[i][j] = matrix.nextInt();
}
}
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++){
for(k = 0; k < rows1; k++){
rSum = rSum + matrix1[i][k] * second[k][j];
}
multiply[i][j] = rSum;
rSum = 0;
}
}
System.out.println("Product of the Matrices: ");
for(i = 0; i < rows; i++){
for(j = 0;j < columns1; j++){
System.out.format("%d\t", multiply[i][j]);
if(j == columns1 - 1){
System.out.print("\n");
}
}
}
}
break;
case 4:
System.out.println("Enter Number of Rows: ");
rows = matrix.nextInt();
System.out.println("Enter Number of Columns: ");
columns = matrix.nextInt();
System.out.println("\nEnter elements of the matrix: \n");
for(i = 0; i < rows; ++i){
for(j = 0; j < columns; ++j){
System.out.format("Enter Element A%d%d: ", i + 1, j + 1);
matrix1[i][j] = matrix.nextInt();
}
}
System.out.println("The Matrix is: ");
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++){
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
int[][] transpose = new int[columns][rows];
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++){
transpose[i][j] = matrix1[j][i];
}
}
System.out.println("Printing Matrix After Transpose: ");
for(i = 0; i < rows; i++){
for(j = 0; j < columns; j++){
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
break;
}
break;
case 16:
System.out.print("\033[H\033[2J");
e = 1;
System.out.println("\nThank You For Visiting\n");
System.exit(0);
break;
}
} calc.close();
}
}