PRACTICE

Recursion

Lesson notes

Your task

Define factorial(n) recursively, with factorial(1) returning 1. Print factorial(5).

Expected output
120
Loading…

OUTPUT

Worked solution
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5))

Checks compare printed output, not your approach. Several solutions can be valid.

← Back to courses