Factorial Of A Number
Summary: Write a PHP program to print the factorial of a number
Write a PHP program to print the factorial of a number

Factorial of a number n is defined by the product of all the numbers from 1 to n (including 1 and n).
Example
Number Is 5
Get Factorial Steps
5*4*3*2*1
Output:
120
Logic:
- Take a number.
- Take the descending positive integers.
- Multiply them.
Program
<?php
$number=6;
$factValue = 1;
for ($i=1; $i<=$number; $i++)
{
$factValue = $factValue * $i;
}
echo "Factorial of $number is $factValue";
?>
Output
Factorial of 6 is 720
Factorial Of A Number Using Function
<?php
function getFactorial($number=""){
$factValue = 1;
for ($i=1; $i<=$number; $i++)
{
$factValue = $factValue * $i;
}
echo "Factorial of $number is $factValue";
}
getFactorial(8);
?>
Output
Factorial of 8 is 40320
Series:
PHP Tutorials Hub
