How to write a recursive function in Python?

81 views

How to write a recursive function in Python?

How to write a recursive function in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

A function that calls itself until a base case is met.

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

print(factorial(5))  # Output: 120

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.