7. Calculate Factorial
EasyLoops~7 min
The factorial of n (written n!) is the product of every whole number from 1 to n.
For example 5! = 5 x 4 x 3 x 2 x 1 = 120. By definition, 0! = 1.
Examples
Example 1
- Input:
- n = 5
- Output:
- 120
Example 2
- Input:
- n = 0
- Output:
- 1
- Why:
- The factorial of zero is defined as 1.
Constraints
0 <= n <= 20The result always fits in a JavaScript number for n <= 20.