Python Practice › Function Programs › Program 19
Factorial function (recursive)
Write a Python function to get the factorial of a number.
Input
5
Output
120
Approach
- n! = n × (n − 1)!, with 0! = 1.
Solution
def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) print(factorial(5))Output
120
Learn the concept
New to this topic? Read the lesson, then come back.