Q:

Write a PHP program to compute and return the square root of a given number

0

Write a PHP program to compute and return the square root of a given number.

Input : 16

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function my_sqrt($n)
{
  $x = $n;
  $y = 1;
  while($x > $y)
  {
    $x = ($x + $y)/2;
    $y = $n/$x;
  }
  return $x;
}
print_r(my_sqrt(16)."\n");
print_r(my_sqrt(14)."\n");
?>

Sample Output:

4                                                           
3.7416573867739

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now