-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Calculator.c
155 lines (129 loc) · 4.37 KB
/
Calculator.c
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
// Simple Calculator in C Language
// Created by:- Robert Taylor
// GitHub:- https://github.com/Rob-Tly
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
// function declarations
int addition();
int subtract();
int multiply();
int divide();
int sq();
int sqrt1();
void exit();
int main()
{
// declaration a local variable op;
int op;
do
{
// displays the multiple operations of the C Calculator
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 Square \t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");
scanf ("%d", &op); // accepts a numeric input to choose the operation
// use switch statement to call an operation
switch (op)
{
case 1:
addition(); /* It call the addition() function to add the given numbers */
break; // break the function
case 2:
subtract(); /* It call the subtract() function to subtract the given numbers */
break; // break the function
case 3:
multiply(); /* It call the multiply() function to multiply the given numbers */
break; // break the function
case 4:
divide(); // It call the divide() function to divide the given numbers
break; // break the function
case 5:
sq(); // It call the sq() function to get the square of given numbers
break; // break the function
case 6:
sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */
break; // break the function
case 7:
exit(0); // It call the exit() function to exit from the program
break; // break the function
default:
printf(" Something is wrong!! ");
break;
}
printf (" \n \n ********************************************** \n ");
} while (op != 7);
return 0;
}
// function definition
int addition()
{
int i, sum = 0, num, f_num; // declare a local variable
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;
}
// use subtract() function to subtract two numbers
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);
}
// use multiply() function to multiply two numbers
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);
}
// use divide() function to divide two numbers
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);
}
// use sq() function to get the square of the given number
int sq()
{
int n1, res;
printf (" Enter a number to get the Square: ");
scanf (" %d", &n1);
res = n1 * n1;
printf (" \n The Square of %d is: %d", n1, res);
}
// use sqrt1() function to get the square root of the given number
int sqrt1()
{
float res;
int n1;
printf (" Enter a number to get the Square Root: ");
scanf (" %d", &n1);
res = sqrt(n1);
printf (" \n The Square Root of %d is: %f", n1, res);
}