The factorial of a number n is defined by the product of all the digits from 1 to n (including 1 and n).
For example,
4! = 4*3*2*1 = 24
6! = 6*5*4*3*2*1 = 720
Note:
- It is denoted by n! and is calculated only for positive integers.
- Factorial of 0 is always 1.
The simplest way to find the factorial of a number is by using a loop.
There are two ways to find factorial in PHP:
- Using loop
- Using recursive method
Logic:
- Take a number.
- Take the descending positive integers.
- Multiply them.
Factorial in PHP
Factorial of 4 using for loop is shown below.
Example:
Output:
Factorial using Form in PHP
Below program shows a form through which you can calculate factorial of any number.
Example:
Output:
Factorial using Recursion in PHP
Factorial of 6 using recursion method is shown.
Example:
Output:

need an explanation for this answer? contact us directly to get an explanation for this answer