Approximating a square root : While loop « Statement « PHP






Approximating a square root

 
<HTML>
<HEAD>
<TITLE>Approximating a square root</TITLE>
</HEAD>
<BODY>
<?php
$target = 81;
$guess = 1.0;
$precision = 0.0000001;

$guess_squared = $guess * $guess;
while (($guess_squared - $target > $precision) or ($guess_squared - $target < - $precision))
{
  print("Current guess: $guess is the square root of $target<BR>");
  $guess = ($guess + ($target / $guess)) / 2;
  $guess_squared = $guess * $guess;
}
print("$guess squared = $guess_squared<BR>");
?>
</BODY>
</HTML>
  
  








Related examples in the same category

1.A sample while loop that counts to 10
2.A while Statement
3.Convert the while statement into a for statement?
4.Create a while statement that prints every odd number between 1 and 49
5.while and loop counter
6.Infinite Loops
7.Fahrenheit and Celsius table by while loop
8.The do...while Statement
9.Printing a
10.A while Statement