Python: Find the Factorial of a Number

Problem Statement:

Write a Python function to find the factorial of a given number.

Solution Explanation:

  • The factorial of a number n is n! = n × (n-1) × (n-2) × ... × 1.
  • We use recursion to calculate the factorial.

Python Code:

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

# Test cases

print(factorial(5)) # Output: 120

print(factorial(7)) # Output: 5040

Leave a Reply