# while loop

Page 1 of 1

## 1 Replies - 1984 Views - Last Post: 09 December 2009 - 05:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144553&amp;s=21b43b25c256f480680189c28e82cf8d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

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

# while loop

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

Reputation: 29
• 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }