Skip to content

Instantly share code, notes, and snippets.

@niravmadariya
Last active June 7, 2018 07:01
Show Gist options
  • Save niravmadariya/bbb7cea761234388f6f7683ab24010a4 to your computer and use it in GitHub Desktop.
Save niravmadariya/bbb7cea761234388f6f7683ab24010a4 to your computer and use it in GitHub Desktop.
Factorial Program in php
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST['submit']){
$fact = 1;
//getting value from input text box 'number'
$number = $_POST['number'];
echo "Factorial of $number:<br><br>";
//start loop
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>
</body>
</html>
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST['submit']){
function fact ($n)
{
if($n <= 1)
{
return 1;
}
else
{
return $n * fact($n - 1);
}
}
echo "Factorial of $_POST['number'] is " .fact($_POST['number']);
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment