Factorial Calculator

Write a Python function that calculates the factorial of a number. The function should accept a number as an argument and return the factorial.

Example 1:

Input: 5 
Output: 120

Example 2:

Input: 3 
Output: 6

Use recursive approach to solve this problem. The factorial of a number n is n times the factorial of (n-1).

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

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!