-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculation_0.c
91 lines (71 loc) · 1.89 KB
/
calculation_0.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
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b);
float absoluteValue(float x);
float squareRoot(float x);
int main()
{
int result = 0;
float f1 = -15.5, f2 = 20.0, f3 = -5.0;
int i1 = -716;
float absoluteValueResult = 0.0;
result = gcd(150, 35);
printf("The gcd of 150 and 35 is %d\n", result);
result = gcd(1026, 405);
printf("The gcd of 1026 and 405 is %d\n", result);
result = gcd(83, 240);
printf("The gcd of 83 and 240 is %d\n\n\n\n", result);
/* testing absoluteValue function */
absoluteValueResult = absoluteValue (f1);
printf ("result = %.2f\n", absoluteValueResult);
printf ("f1 = %.2f\n", f1);
absoluteValueResult = absoluteValue (f2) + absoluteValue (f3);
printf ("result = %.2f\n", absoluteValueResult);
absoluteValueResult = absoluteValue ( (float) i1);
printf ("result = %.2f\n", absoluteValueResult);
absoluteValueResult = absoluteValue (i1);
printf ("result = %.2f\n", absoluteValueResult);
printf ("%.2f\n\n\n", absoluteValue (-6.0) / 4);
/*testing square root*/
printf("%.2f\n", squareRoot(-3.0));
printf("%.2f\n", squareRoot(16.0));
printf("%.2f\n", squareRoot(25.0));
printf("%.2f\n", squareRoot(9.0));
printf("%.2f\n", squareRoot(225.0));
return 0;
}
int gcd(int a, int b)
{
int temp;
while(b != 0)
{
temp = a%b;
a=b;
b=temp;
}
return a;
}
float absoluteValue(float x)
{
if (x<0)
x = -x;
return x;
}
float squareRoot(float x)
{
const float epsilon = .00001;
float guess = 1.0;
float returnValue = 0.0;
if (x<0)
{
printf ("Negative argument to squareRoot.\n");
returnValue = -1.0;
}
else
{
while (absoluteValue (guess*guess-x) >= epsilon)
guess = (x/guess+guess) / 2.0;
returnValue = guess;
}
return returnValue;
}