Back to Practice Dashboard
Python BasicsEasy

Factorial of a number

Learn how to solve the 'Factorial of a number' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function factorial(n) that takes a non-negative integer n and returns its factorial. The factorial of n (written as n!) is the product 1 × 2 × 3 × ... × n. By definition, 0! = 1.

Constraints
  • 0 <= n <= 20

Examples

Example 1
Input
n = 5
Output
120
Explanation

5! = 1 × 2 × 3 × 4 × 5 = 120.

Example 2
Input
n = 0
Output
1
Explanation

0! is defined as 1.

Example 3
Input
n = 10
Output
3628800
Explanation

10! = 3628800.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor