Factorial

Easy~10 min

Given a non-negative integer n, return its factorial (n!).

The factorial of n is the product of all positive integers less than or equal to n. By definition, 0! = 1.

For example, factorial(5) returns 120 because 5 × 4 × 3 × 2 × 1 = 120.

Examples

Example 1
Input: n = 5
Output: 120
Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120.
Example 2
Input: n = 0
Output: 1
Explanation: 0! is defined as 1.
Example 3
Input: n = 1
Output: 1
Explanation: 1! = 1.

Constraints

  • 0 <= n <= 20
  • The result fits in a 64-bit integer.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run