Thursday, 18 April 2013

SQUARE ROOT OF A NUMBER


SQUARE ROOT OF A NUMBER

#include
float squareRoot(float n)
{
/*We are using n itself as initial approximation
This can definitely be improved */
float x = n;
float y = 1;
float e = 0.000001; /* e decides the accuracy level*/
while(x - y > e)
{
x = (x + y)/2;
y = n/x;
}
return x;
}

/* Driver program to test above function*/
int main()
{
int n;
scanf("%d",&n);
printf ("Square root of %d is %f", n, squareRoot(n));
getchar();
}

No comments: