151. PHP Program to find the Binomial Coefficients using recursion.
How does this program work?
- This program is used to find the Binomial Coefficient using recursion in PHP.
- Declare variable r to store reverse number and initialize it with 0.
- Multiply the reverse number by 10, add the remainder which comes after dividing the number by 10.
Here is the code
<html>
<head>
<title> PHP Program To find the Binomial Coefficient recursively</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter n value"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter r value"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
function fact( $num) // Returns factorial of n! ,r!
{
$f = 1;
while($num!=0)
{
$f = $f*$num;
$num--;
}
return $f;
}
if(isset($_POST['submit'])) {
$n = $_POST['num1'];
$r = $_POST['num2'];
// To compute NcR using n!/(n-r)!*r!
$NcR = fact($n) / (fact($r) * fact($n - $r));
echo "The NcR value of $n and $r is: " .$NcR;
return 0;
}
?>
</body>
</html>