Estimate a square root of a number n:
1.make a guess (you can pick n/2);
2.compute r=n/guess;
3.set guess=(guess+r)/2;
4.Go back for step 2 and 3,until the guess is within 1% of the previous guess.the more,the closer.
OK,so we need a program to do it!
Code:
#include<iostream>
using namespace std;
int main()
{
double n,r,gue,gue1;
cout<<"enter n:";
cin>>n;
gue=n/2;
do
{
r=n/gue;
gue1=gue;
gue=(gue+r)/2;
}while(gue>=0.01*gue1)
cout<<"It is "<<gue;
return 0;
}
Look,I make gue1 to be the previous guess,so we can stop the program,but when I run it,it
is a infinite loop.Why?