while loop

question about while loop

Page 1 of 1

1 Replies - 1276 Views - Last Post: 09 December 2009 - 05:57 PM Rate Topic: -----

#1 Micro24/7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 09

while loop

Post icon  Posted 09 December 2009 - 05:33 PM

How do you compute a square root with a while loop
Is This A Good Question/Topic? 0
  • +

Replies To: while loop

#2 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: while loop

Posted 09 December 2009 - 05:57 PM

An easy way is using the Newton–Raphson method,

Which states x1 = x0 - f(x0)/f'(x0)

x0 is given a starting value and the next estimate x1 will give a closer value, then it is run again with x0=x1, when x0 == x1 the root has been found.

So when you are trying to find the square root,

So f(x0) is x0*x0
And f'(x0) is 2* x0

So a do while loop would look like this

double number;//number to find sqaure root of
double x0, x1;
x1= number/2;//give it a first estimate to start at given to x0 in do while loop
do{
x0 = x1;//update the estimate
x1 = x0- (x0*x0)/(2*x0);
}
while(x0 != x1);
//x0 or x1 will contain the value



Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1