Factorial Program in php

What is factorial?

Factorial is a part of mathematics. It is a positive number and it is multiplication of the all positive numbers and move to 1.


5!= 5*4*3*2*1
  = 120

Factorial program without any predefined method?

We can get factorial of any number


<?php 
function factorial($n)
{
	$fact=1;
	for($i=$n; $i>=1;$i--)
	{
	$fact=$fact*$i;	
	}
	return $fact;
}
echo factorial(4) //Output:- 24
echo factorial(5) //Output:- 120
?>

Try it Yourself

Factorial program through recursion

Factorial Program through recursion of any number


<?php 
function Recu_factorial($number)
{
	if($number==0)
	{
		return 1;
	}
	return $number * Recu_factorial($number - 1);
}

echo Recu_factorial(4); //Output:- 24
echo Recu_factorial(5); //Output:- 120
?>

Try it Yourself