#include #include #include #define PRECISION pow( 10, -6 ) float mysqrt( float, float ); int main( ){ float x = 0.0; for( ;; ) { printf( "Enter number: " ); scanf( "%f", &x ); if( x < 0 ){ printf( "Number is less than 0, exiting cowardly!\n" ); exit( 0 ); } printf( "Square root: %f\n", mysqrt( x, PRECISION ) ); } } /** * Function for calculating the square root of a given number "r" * with a precision of "e" * * @param float r number whose root to find * @param float e root precision * * @return float the root of r */ float mysqrt( float r, float e ){ float x = r / 2.0, x_last = 0; while( fabs( x - x_last ) / x > e ){ x_last = x; x = 0.5 * ( x + r / x ); } return x; }