-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCalculator.cpp
99 lines (91 loc) · 2.2 KB
/
Calculator.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
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int addition();
int subtract();
int multiply();
int divide();
void exit();
int main()
{
int op;
do
{
printf (" Select an operation to perform the calculation in C Calculator: ");
printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Exit \n 6 Please, Make a choice ");
scanf ("%d", &op);
switch (op)
{
case 1:
addition(); /* It call the addition() function to add the given numbers */
break; // break the function
case 2:
subtract();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
exit(0);
break;
default:
printf(" Something is wrong!! ");
break;
}
printf (" \n \n ********************************************** \n ");
} while (op != 5);
return 0;
}
int addition()
{
int i, sum = 0, num, f_num;
printf (" How many numbers you want to add: ");
scanf ("%d", &num);
printf (" Enter the numbers: \n ");
for (i = 1; i <= num; i++)
{
scanf(" %d", &f_num);
sum = sum + f_num;
}
printf (" Total Sum of the numbers = %d", sum);
return 0;
}
int subtract()
{
int n1, n2, res;
printf (" The first number is: ");
scanf (" %d", &n1);
printf (" The second number is: ");
scanf (" %d", &n2);
res = n1 - n2;
printf (" The subtraction of %d - %d is: %d", n1, n2, res);
}
int multiply()
{
int n1, n2, res;
printf (" The first number is: ");
scanf (" %d", &n1);
printf (" The second number is: ");
scanf (" %d", &n2);
res = n1 * n2;
printf (" The multiply of %d * %d is: %d", n1, n2, res);
}
int divide()
{
int n1, n2, res;
printf (" The first number is: ");
scanf (" %d", &n1);
printf (" The second number is: ");
scanf (" %d", &n2);
if (n2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2;
printf (" \n The division of %d / %d is: %d", n1, n2, res);
}