PHP program to enter any number and calculate its square root
ADVERTISEMENTS
PHP program to enter any number and calculate its square root.
There are you will learn how to find the square root of any number in the PHP language.
Formula:
r = n^(1/2)
where:
r = root
n = number
There are two ways to implement these formulae:
- By using the sqrt() function
- Second, by using the pow() function
1. By using the sqrt() function
<?php
// PHP program to calculate the square root of a number
$n = 25;
$r = NULL;
// n = number
// r = root
// Calculate square root of number
$r = (double)sqrt($n);
// Output
echo "Square root of " . $n . " = " . $r;
?>
Output:
Square root of 25 = 5
2. By using the pow() function
<?php
// PHP program to calculate the square root of a number using the pow() function
$n = 25;
$r = NULL;
// n = number
// r = root
// Calculate square root of number
$r = (double)pow($n, 0.5);
// Output
echo "Square root of " . $n . " = " . $r;
?>
Output:
Square root of 25 = 5