Home > Backend Development > PHP Tutorial > Square Root in PHP

Square Root in PHP

WBOY
Release: 2024-08-29 13:12:46
Original
523 people have browsed it

Calculating other roots like the nth root of a number, or cube root of a number, similarly, we need to find the square root of numbers in PHP. We calculate these roots by using different functions like pow(), log() and others.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

In a programming language like PHP, calculating square root is simple when used with built-in function. This function is sqrt(). We will also see how to find the square root of a number without using sqrt() and how to calculate square root using a form with user input.

The sqrt() function is used to calculate the square root of a given number. This function is a built-in Math function used in PHP like pow(), rand(), is_nan() etc.

Square Root Logic

The syntax and description of square root logic is explained in details below,

Syntax:

sqrt($num)
Copy after login

Where $num is the single argument passed to the sqrt function.

Description: sqrt() function calculates and returns the square root of the given number. The returned value is of type float. Also, we have different types of input numbers to the given function on which the square root function is performed and the result is calculated.

Here we will see that the input numbers can be positive or negative numbers or decimal numbers (float) or it can also be zero. The positive numbers return positive numbers as output and negative numbers return NAN (Not a Number) as output, the square root of decimal numbers is a float as output, and the square root of one is one. Also, remember the square root of zero is zero.

Finding Square Root of a Given Number

The square root of a given number is as per the following,

If the input number is 81, the square root of the number will be 9. If the input number is 49, the square root number will be 7 and so on.

Let us learn this with an example:

We will also learn to find the square root with different types of input.

Example #1

Code:

<?php
// simple example to find how sqrt() function works on numbers
echo sqrt(16);
echo '<br>';
// output is 4
echo sqrt(7);
echo '<br>';
//output is 2.6457513110646
?>
Copy after login

Output:

Square Root in PHP

In the above program, the output is 4 as we know 4*4 is 16 thus the square root of 16 is 4. While calculating the square root of 7, we see that after the decimal many digits are found, the number of digits after the decimal depends upon the user.

Similar to the sqrt function, which calculates the square root of the given number. To calculate any root of the given number we use pow() function which stands for power.

Example #2

Code :

<?php
// example to calculate any root
echo '<br>'.'Result of  :   pow(16, 1/2)  ======  '. pow(16, 1/2);
// example to calculate the cube root of 27
echo '<br>'.'Result of  : pow(27, 1/3)  ======  '. pow(27, 1/3);
//example to calculate the fourth root of 12
echo '<br>'.'Result of  : pow(12, 1/4)  ======  '. pow(12, 1/4);
//example to calculate the fifth root of 76
echo '<br>'.'Result of  : pow(76, 1/5)  ======  '. pow(76, 1/5);
//example to calculate the sixth root of 88
echo '<br>'.'Result of  : pow(88, 1/6)  ======  '. pow(88, 1/6);
?>
Copy after login

Output:

Square Root in PHP

Example #3

Code:

<?php
echo '<br>'.'Result of  :   sqrt(625)  ======  '. sqrt(625);
echo '<br>'.'Result of  :   sqrt(49)  ======  '. sqrt(49);
echo '<br>'.'Result of  :   sqrt(-36)  ======  '. sqrt(-36);
echo '<br>'.'Result of  :   sqrt(0)  ======  '. sqrt(0);
echo '<br>'.'Result of  :   sqrt(121)  ======  '. sqrt(121);
echo '<br>'.'Result of  :   sqrt(22)  ======  '. sqrt(22);
echo '<br>'.'Result of  :   sqrt(12.34)  ======  '. sqrt(12.34);
echo '<br>'.'Result of  :   sqrt(-16)  ======  '. sqrt(-16);
?>
Copy after login

Output:

Square Root in PHP

Example #4

Finding Square Root of A Number Entered by The User Through a Form: In the following program, we have created a program in PHP to calculate the square root of a number entered by the user through a form. Suppose the user has entered 16 then we can find the square root of the 16 and expect the result as 4, if the user entered 49 we can expect the result as 7 and so on.

Also, we have used the built-in mathematical function sqrt() to find the square root.

Code:

<!---program to calculate square root of input number using form -->
<html>
<head>
<title>Square root of a number using form</title>
</head>
<body>
<!--- input form with text box --->
<form method="post" action="">
<label>Enter a number</label>
<input type="text" name="input" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])) {
//storing the number in a variable $input
$input = $_POST['input'];
//storing the square root of the number in a variable $ans
$ans = sqrt($input);
//printing the result
echo 'The square root of '.$input.'====='.$ans;
}
?>
</body>
</html>
Copy after login

Output – 1:

Square Root in PHP

Output – 2: With 100 as input.

Square Root in PHP

Example #5

Finding Square Root of A Number without Using Built-in sqrt() Function: In the following program, we have created a program in PHP to calculate the square root of a number without using built-in sqrt() function.

Code:

function squareroot($input)
{
//if the input number is 0 then return 0 as result
if($input == 0) {
return 0;
}
//if the input number is 1 then return 1 as result
if($input == 1) {
return 1;
}
// assigning $input value to a variable $a
$a = $input;
$b = 1;
while($a > $b)
{
// calculating the middle number
$a= ($a + $b)/2;
// dividing the input number with the middle number
$b = $input/$a;
}
return $a;
}
echo '<br>'.'Square root of 0 is '.squareroot(0);
echo '<br>'.'Square root of 20 is '.squareroot(20);
echo '<br>'.'Square root of 49 is '.squareroot(49);
echo '<br>'.'Square root of 81 is '.squareroot(81);
echo '<br>'.'Square root of 1 is '.squareroot(1);
Copy after login

Output:

Square Root in PHP

Conclusion

In this article, we learned what square root is, how do we calculate square roots with and without the built-in functions like sqrt(), pow(). What the sqrt() and pow() function does, how is it used in a program to find the square root? We learned about performing square root on numbers, floating-point numbers, negative numbers and so on. We also learned about calculating square root with user-defined input using form.

The above is the detailed content of Square Root in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template