Factorial function (recursive)

EasyFunction Programs · 19 of 39

Write a Python function to get the factorial of a number.

Advertisement
Input
5
Output
120

Approach

  1. n! = n × (n − 1)!, with 0! = 1.

Solution

Python
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.

More function programs

← All 39 Function Programs

Leave a Comment