-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
294 lines (149 loc) · 4.76 KB
/
functions.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
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
#include <stdio.h>
void checkOddOrEven(){
int input;
printf("Enter Number\n\n>>");
scanf("%d", &input);
if(input % 2 == 0){
printf("This is even\n\n");
}else{
printf("This is odd\n\n");
}
}
void checkPrime(){
int input,i;
printf("Enter Number\n\n>>");
scanf("%d", &input);
for (i=2; i < input; i++) {
if (input % i == 0 && i != input) {
printf("not a prime number");
}
}
printf("a prime number");
}
void checkDivisbility(){
int input1,input2;
printf("Enter Number\n\n>>");
scanf("%d", &input1);
printf("Enter number to be divided with\n\n>>");
scanf("%d", &input2);
if (input1 % input2 == 0) {
printf("%d is divisble by %d", input1, input2);
}else{
printf("%d is not divisble by %d", input1, input2);
}
}
void checkGoldenRatio(){
int input1,input2,output;
printf("Enter Number\n\n>>");
scanf("%d", &input1);
printf("Enter Number \n\n>>");
scanf("%d", &input2);
if(input2 > input1){
printf("Invalid!!\n\n");
}else{
output = input1 / input2;
if(output > 1.68 && output < 1.69){
printf("Golden Ratio Exists\n\n");
}else{
printf("Golden Ratio doesn't exist \n\n");
}
}
}
void ComputeFactorial(){
int input,i,original,output;
printf("Enter Number\n\n>>");
scanf("%d", &input);
original = input;
for(i = 1; i<=input; i++){
output *= i;
}
printf("%d! = %d", original , output);
}
void ComputeFibonacci(){
int input,i,output;
printf("1- Compute nth term\n2- Compute Fibonacci till nth term\n\n\n Enter Choice\n>>");
scanf("%d", &input);
if(input == 1){
printf("Enter Number\n\n>>");
scanf("%d", &input);
printf("The %dth term Fibonacci is %dof \n\n>>", input, Fibonacci(input));
} else if(input == 2) {
printf("Enter Number\n\n>>");
scanf("%d", &input);
printf("Fibonacci till %dth term\\n\n>>", input);
for(i = 0 ; i<= input; i++){
printf("%d",Fibonacci(i));
}
}
}
//Using Recursion
int Fibonacci(int n) {
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
void ComputePi(){
/**
* Gauss–Legendre algorithm
*
* pi = (a + b)*(a + b)/(4*t)
*
* first 3 iterations to get accurate
*
*/
double a = 1.0; //a = 1
double b = 1.0/sqrt ( 2.0 ); //b = cos(45)
double t = 1.0/4.0; //t = 0.25
double x = 1.0; //P = 1
double aDash; // to equalize a;
double pi;
int i;
for ( i = 0; i < 4; i++ ){
aDash = a;
a = (a + b)/2.0;
b = sqrt ( b*aDash );
t -= x*(aDash - a)*(aDash - a);
x *= 2;
}
pi = (a + b)*(a + b)/(4*t);
printf( "Pi = %f\n", pi );
}
void QuadraticEquation(){
int a,b,c;
double resultp,resultm;
printf("Enter coffecients\n\n>>");
printf("a = ");
scanf("%d", &a);
printf("\nb = ");
scanf("%d", &b);
printf("\nc = ");
scanf("%d", &c);
resultp = (double) (-b + sqrt( pow((double)b, (double)2) - (4 * a * c) )) / ( 2*a);
resultm = (double) (-b - sqrt( pow((double)b, (double)2) - (4 * a * c) )) / ( 2*a);
printf("Roots are %f and %f", resultp,resultm);
}
void Logarithm(){
int input,output;
double i,x;
printf("1-Compute log base 10 \n2- calculate natural logarithm \n3-Custom Log \n\n Enter Choice\n>>");
scanf("%d", &input);
if(input == 1){
printf("Enter Number\n\n>>");
scanf("%d", &input);
printf("Logarithm %d to the base 10 = %f", input, log10(input));
}else if (input == 2){
printf("Enter Number\n\n>>");
scanf("%d", &input);
printf("Logarithm %d to the base e = %f", input, log(input));
}else if (input == 3){
printf("Enter base\n\n>>");
scanf("%d", &input);
printf("Enter number\n\n>>");
scanf("%d", &x);
i = log(input) / log(x);
printf("Logarithm %d to the base e = %f", input, log(i));
}
}