Home >>PHP Programs >PHP Program to print the factorial of a given number

PHP Program to print the factorial of a given number

PHP Program to print the factorial of a given number

In this example, we will create a PHP program to print the factorial of a given number. The factorial of a number n is defined by the product of all the digits from 1 to n.

PHP Factorial Program Example 1

<?php  
$num = 7;  
$factorial = 1;  
for ($x=$num; $x>=1; $x--)   
{  
  $factorial = $factorial * $x;  
}  
echo "Factorial of $num is $factorial";  

?> 

Output:-
Factorial of 7 is 5040
PHP Factorial Program Example 2

<?php 
extract($_REQUEST);
if(isset($check))
{
$factorial = 1;  
for ($x=$num; $x>=1; $x--)   
{  
  $factorial = $factorial * $x;  
}  
echo "Factorial of $num is $factorial";
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Form</title>
	</head>
	<body>
		<form method="post">
		<table>
		<tr>
		<td>Enter Your Number</td>
		<td><input type="text" name="num" required/></td>
		</tr>
		<td colspan="2" align="center">
			<input type="submit" value="Print Factorial" name="check"/>
		</tr>
		</form>
	</body>
</html>
</html>

Output:-
Print Factorial