Question #17834

Write a function that calculates the square root of a
number

Expert's answer

#include <iostream>

using namespace std;

double sqaure_root_of(double value)

{

double lo = 1.0;

double hi = value;

while( hi - lo > 0.00001)

{

double mid = lo + (hi -lo) / 2 ;

if( mid * mid - value> 0.00001)

{

hi = mid;

} else {

lo = mid;

}

}

return lo;

}

int main ()

{

cout<< sqaure_root_of(144)<< endl;

system("pause");

return0;

}

using namespace std;

double sqaure_root_of(double value)

{

double lo = 1.0;

double hi = value;

while( hi - lo > 0.00001)

{

double mid = lo + (hi -lo) / 2 ;

if( mid * mid - value> 0.00001)

{

hi = mid;

} else {

lo = mid;

}

}

return lo;

}

int main ()

{

cout<< sqaure_root_of(144)<< endl;

system("pause");

return0;

}

## Comments

## Leave a comment