/* sqrt.c - Compute the square root of a number
	    using Heron's method
 */

#include <stdio.h>
#include <math.h>

#define EPSILON 1E-12

double getSqrt(double a) {
    double guess = 1;
    double nextGuess = a;

    while ((nextGuess != 0.0) && (fabs(guess - nextGuess) > EPSILON)) {
	guess = nextGuess;
	nextGuess = (guess + a / guess)/2.0;
    }
    return nextGuess;
}

int main() {
    double w;
    printf("Enter a number: ");
    scanf("%lf", &w);
    printf("The square root of %lf is %lf\n", w, getSqrt(w));
    return 0;
}

