144. PHP Program to find the Factorial of a given number using recursion.
How does this program work?
- This program is used to find the factorial of a given number using recursion in PHP.
- Factorial number is defined by the product of all the digits of given number.
- Factorial of 0 is always 1.
- Recursion method is used to find the factorial of a given number.
Here is the code
<html>
<head>
<title>PHP Program To find the Factorial of a given number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
function Factorial($number) {
$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial = $factorial * $i;
}
return $factorial;
}
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$x = Factorial($n);
echo "Factorial of number ". $n . " is: " .$x;
return 0;
}
?>
</body>
</html>