Hi
Try the below code
// Square root approximation - Babylonian method
double sqroot(const double s) {
double xn = s / 2.0;
double lastX = 0.0;
// Looping this way ensures we perform only as many calculations as necessary.
// Can be replaced with a static for loop if you really want to.
while(xn != lastX) {
lastX = xn;
xn = (xn + s/xn) / 2.0;
}
return xn;
}