**Solution :**

This is very tricky question for any programmer. I have also asked this question in many interviews but most of the time I got wrong answers from the candidates. So I am trying to answer it for you.

Below is the code which needs only 6 iterations to meet to the maximum precision for the double numbers:

#include <math.h>

double SquareRoot(double x) {

if (x <= 0)

return 0; // if in case the negative number throws the exception

int exp = 0;

x = frexp(x, &exp); // extract the binary exponent from the x

if (exp & 1) { // to get the exponent to be even

exp--;

x *= 2;

}

double y = (1+x)/2; // this is our first approximation

double z = 0;

while (y != z) { // we are compare doubles

z = y;

y = (y + x/y) / 2;

}

return ldexp(y, exp/2); //we can have multiply answer by 2^(exp/2)

}