20 July 2010

Newton-Raphson Method to Compute the Square Root in C



#include
#include

float square_root(float);

int main(void)
{

printf("square_root of %f is %f\n", 9.0, square_root(9.0));

return 0;
}

/*Newton-Raphson Method to Compute the Square Root of x*/
float square_root(float n)
{
const float epsilon = 0.00001;
float guess = 1.0;

while (fabs(guess*guess - n) >= epsilon)
guess = (n/guess + guess)/2.0;

return guess;
}

Post a Comment