4. PHP Program to find Square root of a given number.
How does this program work?
- In this program you will learn about how to do Square root of a given number using PHP.
- The following code uses sqrt() mathematical function.
- And then by using the given logic you will get output. If number is negative, the sqrt function will return a domain error.
Here is the code
<html>
<head>
<title>PHP Program To find Square root of a number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td><input type="text" name="num1" value="" placeholder="Enter a value"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['num1'];
//To find square root of a number using sqrt() function
$SquareRoot = sqrt($a);
$precision = 4;//It displays 4 digits after decimal point.
echo "SquareRoot of a number " .$a." is "." = ".number_format( $SquareRoot, $precision);
return 0;
}
?>
</body>
</html>