/*
* app_sqrt.c
* This program reads a positive number and
approximates is square root.
*/
#include <stdio.h>
int main ()
{
float x;
float app;
const
float epsilon = 0.0001;
/* Read a
non-negative number. */
do
{
printf ("Please enter a positive number: ");
scanf ("%f", &x);
} while (x
<= 0);
/* Approximate the root using an ancient Babylonian technique.
*/
app = x/2;
/* Initial guess. */
do
{
/* app + x/app
* Refine the approximation by computing:
app = -------------
*
2
*/
app = (app +
x/app)/2;
/* We proceed until (|app^2 - x| < epsilon): */
} while (!
((app*app - x < epsilon) && (app*app - x > -epsilon)));
/* Print the
result. */
printf ("The square root of %g is approximately %g\n",
x, app);
return
(0);
}