Sample – compute square roots using Newton’s method

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>
#include <math.h>

float asqrt (float x, float precision) {
float guess;

guess = 1.0;
while (fabs(guess*guess-x) >= precision) {
guess = 0.5 * (guess + (x / guess));
}
return (guess);
}

int main () {
float x, precision;
cout << “Enter a real number and the precision: “;
cin >> x >> precision;
cout << “sqrt(” << x << “) is almost ” << asqrt(x,precision) << endl;
return(0);
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – convert temperatures
  2. Sample – factorial
  3. Sample – exponentiation using multiplication
  4. Sample program (multiplication using addition)
  5. Sample – Fibonacci numbers

Leave a Comment

Previous post:

Next post: