-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.cpp
563 lines (495 loc) · 11.9 KB
/
cal.cpp
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Scientific Calculator Program (in C)
// Header Files
#include <stdio.h>
#include <math.h> // for Trigonometric, Logarithmic and Exponential function
#define PI 3.141592654
// Global variables to hold Results
long long intResult = 0;
double k = 0, result = 0;
// Menu function (to be called in switch)
int menu()
{
int ch;
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n5. Remainder");
printf("\n6. Factorial");
printf("\n7. Sine");
printf("\n8. Cosine");
printf("\n9. Tangent");
printf("\n10.log(base e)");
printf("\n11.log(base 10)");
printf("\n12.e^x");
printf("\n13.SquareRoot");
printf("\n14.CubeRoot");
printf("\n15.Power");
printf("\n16.Absolute Value");
printf("\n17.Sine Inverse");
printf("\n18.Cosine Inverse");
printf("\n19.Tangent Inverse");
printf("\n20.Ceil Function");
printf("\n21.Floor Function");
printf("\n22.Permutation (nPr)");
printf("\n23.Combination (nCr)");
printf("\n24.Clear");
printf("\n25.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
return ch;
}
// Function to add numbers
void addition()
{
double a, b;
if(k)
{
printf("\nEnter a number: ");
scanf("%lf", &a);
result += a;
printf("\nResult = %lf", result);
}
else
{
printf("\nEnter two numbers: ");
scanf("%lf%lf", &a, &b);
result = a + b;
printf("\nResult = %lf", result);
}
}
// Function to subtract numbers
void subtraction()
{
double a, b;
if(k)
{
printf("\nEnter a number: ");
scanf("%lf", &a);
result -= a;
printf("\nResult = %lf", result);
}
else
{
printf("\nEnter two numbers: ");
scanf("%lf%lf", &a, &b);
result = a - b;
printf("\nResult = %lf", result);
}
}
// Function to multiply numbers
void multiplication()
{
double a, b;
if(k)
{
printf("\nEnter a number: ");
scanf("%lf", &a);
result *= a;
printf("\nResult = %lf", result);
}
else
{
printf("\nEnter two numbers: ");
scanf("%lf%lf", &a, &b);
result = a * b;
printf("\nResult = %lf",result);
}
}
// Function to divide numbers
void division()
{
double a, b;
if(k)
{
printf("\nEnter a number: ");
scanf("%lf", &a);
if(a!=0)
{
result /= a;
printf("\nResult = %lf", result);
}
else
{
printf("Math Error\n");
}
}
else
{
printf("\nEnter two numbers: ");
scanf("%lf%lf", &a, &b);
if (b!=0)
{
result = a / b;
printf("\nResult = %lf", result);
}
else
{
printf("Math Error\n");
}
}
}
// Function to find remainder
void mod()
{
long long a, b;
if(k)
{
printf("\nEnter a number: ");
scanf("%lld", &a);
intResult %= a;
printf("\nResult = %d",intResult);
}
else
{
printf("\nEnter two numbers: ");
scanf("%lld%lld", &a, &b);
intResult = a % b;
printf("\nResult = %lld", intResult);
}
}
// Function to calculate factorial of a number
void factorial()
{
long long n, f, i;
printf("\nEnter a number: ");
scanf("%lld", &n);
f = 1;
for(i = 1; i<=n; i++)
{
f = f * i;
}
intResult = f;
printf("\nResult = %lld", intResult);
}
long long factorialReturn(long long n)
{
long long f, i;
f = 1;
for(i = 1; i<=n; i++)
{
f = f * i;
}
return f;
}
// Function to calculate sine of angle in radians
void sine()
{
double a;
printf("Enter angle in radians: ");
scanf("%lf", &a);
result = sin(a);
printf("\nResult = %lf", result);
}
// Function to calculate cosine of angle in radians
void cosine()
{
double a;
printf("Enter angle in radians: ");
scanf("%lf", &a);
result = cos(a);
printf("\nResult = %lf", result);
}
// Function to calculate tangent of angle in radians
void tangent()
{
double a;
printf("Enter angle in radians: ");
scanf("%lf", &a);
result = tan(a);
printf("\nResult = %lf", result);
}
// Function to calculate log (base e)
void logBasee()
{
double a;
printf("Enter a number: ");
scanf("%lf", &a);
if(a<=0.0)
{
printf("Math Error\n");
}
else
{
result = log(a);
printf("\nResult = %lf", result);
}
}
// Function to calculate log (base 10)
void logBase10()
{
double a;
printf("Enter a number: ");
scanf("%lf", &a);
if(a<=0.0)
{
printf("Math Error\n");
}
else
{
result = log10(a);
printf("\nResult = %lf", result);
}
}
// Function to calculate e^x
void eToPowerX()
{
double a;
printf("Enter a number: ");
scanf("%lf", &a);
result = exp(a);
printf("\nResult = %lf", result);
}
// Function to find the Square Root of a Number
void squareRoot()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
if (n<0)
{
printf("Math Error\n");
}
else
{
result = sqrt(n);
printf("\nResult = %lf", result);
}
}
// Function to find the Cube Root of a Number
void cubeRoot()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
result = cbrt(n);
printf("\nResult = %lf", result);
}
// Function to find the Power of a Number
void power()
{
double base, expo;
printf("Enter a base number: ");
scanf("%lf", &base);
printf("Enter an exponent: ");
scanf("%lf", &expo);
result = pow(base, expo);
printf("%.1lf^%.1lf = %.2lf", base, expo, result);
}
// Function to find the Absolute Value of a Number
void absolute()
{
int n;
printf("\nEnter a number: ");
scanf("%lld",&n);
intResult = abs(n);
printf("\nResult = %lld", intResult);
}
// Function to compute the arc sine(inverse sine) of an argument
void sineInverse()
{
double n;
printf("\nEnter a number: ");
scanf("%lf",&n);
if(n>1 || n<-1) // Parameter not in Range
{
printf("Not in Range");
}
else
{
result = asin(n);
printf("Inverse of sin(%.2f) = %.2lf in radians\n", n, result);
// converting radians to degree
result = asin(n)*180/PI;
printf("Inverse of sin(%.2f) = %.2lf in degrees\n", n, result);
}
}
// Function to compute the arc cosine(inverse cosine) of an argument
void cosineInverse()
{
double n;
printf("\nEnter a number: ");
scanf("%lf",&n);
if(n>1 || n<-1) // Parameter not in Range
{
printf("\nNot in Range");
}
else
{
result = acos(n);
printf("\nInverse of cos(%.2f) = %.2lf in radians\n", n, result);
// converting radians to degree
result = acos(n)*180/PI;
printf("\nInverse of cos(%.2f) = %.2lf in degrees\n", n, result);
}
}
// Function to compute the arc tangent(inverse tangent) of an argument
void tangentInverse()
{
double n;
printf("\nEnter a number: ");
scanf("%lf",&n);
result = atan(n);
printf("\nInverse of tan(%.2f) = %.2f in radians", n, result);
// Converting radians to degrees
result = (result * 180) / PI;
printf("\nInverse of tan(%.2f) = %.2f in degrees", n, result);
}
// This function gives the smallest integer that is greater than or equal to Number
void ceilF()
{
double n;
printf("\nEnter a number: ");
scanf("%lf",&n);
result = ceil(n);
printf("Ceiling integer of %.2f = %f", n, result);
}
// This function gives the largest integer that is smaller than or equal to Number
void floorF()
{
double n;
printf("\nEnter a number: ");
scanf("%lf",&n);
result = floor(n);
printf("Floor integer of %.2f = %f", n, result);
}
// Function to calculate Permutations (nPr)
void npr()
{
long long n, r;
printf("\nEnter two numbers: ");
scanf("%lld%lld", &n, &r);
if(n>=r && n>0 && r>=0)
{
intResult = factorialReturn(n) / factorialReturn(n - r);
printf("\nResult = %lld", intResult);
}
else
{
printf("Math Error\n");
}
}
// Function to calculate Combinations (nCr)
void ncr()
{
long long n, r;
printf("\nEnter two numbers: ");
scanf("%lld %lld", &n, &r);
if(n>=r && n>0 && r>=0)
{
intResult = factorialReturn(n) / (factorialReturn(r) * factorialReturn(n - r));
printf("\nResult = %lld", intResult);
}
else
{
printf("Math Error\n");
}
}
// Function to reset global variables
void clear()
{
printf("\nOld Data Cleared");
intResult = 0;
result = 0;
k = 0;
}
// MAIN
int main()
{
int l = 0;
// Main execution loop
while(1)
{
// Displaying the current results
printf("\n Old Decimal Result = %f", result);
printf("\n Old Integer Result = %d", intResult);
// Switch Menu
switch(menu())
{
case 1: addition();
k = 1;
break;
case 2: subtraction();
k = 1;
break;
case 3: multiplication();
k = 1;
break;
case 4: division();
k = 1;
break;
case 5: mod();
k = 1;
break;
case 6: factorial();
k = 1;
break;
case 7: sine();
k = 1;
break;
case 8: cosine();
k = 1;
break;
case 9: tangent();
k = 1;
break;
case 10:logBasee();
k = 1;
break;
case 11:logBase10();
k = 1;
break;
case 12:eToPowerX();
k = 1;
break;
case 13:squareRoot();
k = 1;
break;
case 14:cubeRoot();
k = 1;
break;
case 15:power();
k = 1;
break;
case 16:absolute();
k = 1;
break;
case 17:sineInverse();
k = 1;
break;
case 18:cosineInverse();
k = 1;
break;
case 19:tangentInverse();
k = 1;
break;
case 20:ceilF();
k = 1;
break;
case 21:floorF();
k = 1;
break;
case 22:npr();
k = 1;
break;
case 23:ncr();
k = 1;
break;
case 24:clear();
break;
case 25:l = 1;
break;
default:
printf("\nInvalid Choice !");
}
// Waiting for a button to be pressed
printf("\nPress any button to continue......");
//getch();
// Clear screen command to clear screen after each menu iteration
system("cls");
// To break out of this menu loop
if(l == 1)
break;
return 0;
}
}