Write a program to generate a factorial of any number in php

In this article, We’ll write a program to generate a factorial of any number in php.

first, we understand the meaning of factorial

in mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

For example:

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




Below an example:

1
2
3
4
5
6
7
$number = 5;
$fact   = 1;
while($number > 0) {
   $fact = $fact * $number;
   $number--;
}
 echo $fact; // Output 120

In above, we wrote a program to generate the factorial of 5 and the factorial of 5 came 120.

That’s it!. Please share your thoughts or suggestions in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *