We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e41f105 commit 5e5d6cfCopy full SHA for 5e5d6cf
prog.8.8.c
@@ -0,0 +1,32 @@
1
+// Function to calculate the absolute value of a number
2
+
3
+#include <stdio.h>
4
5
+float absoluteValue (float x)
6
+{
7
+ if ( x < 0 )
8
+ x = -x;
9
+ return (x);
10
+}
11
12
+// Function to compute the square root of a number
13
14
+float squareRoot (float x)
15
16
+ const float epsilon = .00001;
17
+ float guess = 1.0;
18
19
+ while ( absoluteValue (guess * guess - x) >= epsilon )
20
+ guess = ( x / guess + guess ) / 2.0;
21
22
+ return guess;
23
24
25
+int main (void)
26
27
+ printf ("squareRoot (2.0) = %f\n", squareRoot (2.0));
28
+ printf ("squareRoot (144.0) = %f\n", squareRoot (144.0));
29
+ printf ("squareRoot (17.5) = %f\n", squareRoot (17.5));
30
31
+ return 0;
32
0 commit comments