Back to Python
2026-03-166 min read

Python Program to Find Factorial of Number Using Recursion

Learn Python Program to Find Factorial of Number Using Recursion step by step with clear examples and exercises.

Title: Python Program to Find Factorial of Number Using Recursion

Why This Matters

In programming, factorials are essential mathematical functions used in various calculations such as probability, combinatorics, and statistics. The factorial of a number is the product of all positive integers less than or equal to that number. For instance, the factorial of 5 (5!) is 1*2*3*4*5 = 120.

Understanding recursion is crucial for writing efficient programs, as it allows you to solve complex problems by breaking them down into smaller, more manageable sub-problems. In this lesson, we will learn how to write a Python program that calculates the factorial of a number using recursion, which can be used in multiple real-world scenarios like coding competitions and solving mathematical problems.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the following concepts:

  1. Python programming basics (variables, data types, functions)
  2. Understanding of loops and conditional statements (if-else)
  3. Familiarity with recursion as a problem-solving approach
  4. Basic knowledge of function definitions and calls in Python
  5. Understand the concept of a base case in recursive functions
  6. Have experience working with variables and arithmetic operations in Python

Core Concept

In Python, we can write a function to calculate the factorial of a number using recursion. The recursive function calls itself repeatedly until it reaches the base case, which is when the input number becomes 1 or 0. At that point, the function returns 1 (since the factorial of 0 and 1 is 1).

Here's a simple example of how to implement this in Python:

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

num = 7
result = recur_factorial(num)
print("Factorial of", num, "is:", result)

In this code snippet, the recur_factorial() function takes an integer n as input and checks if it is equal to 1 or 0. If so, it returns 1, which serves as the base case for our recursion. Otherwise, it multiplies n with the result of calling recur_factorial(n-1). This process continues until the function reaches the base case and starts returning values back up the call stack.

Understanding Recursive Calls

To better understand how recursive calls work, let's trace the execution of the example above for calculating the factorial of 7:

  1. recur_factorial(7) is called with input n = 7.
  2. The function checks if n == 1 or n == 0, but it's not, so it performs the recursive call: 7 * recur_factorial(6).
  3. Now, we have a new call to recur_factorial(6) with input n = 6.
  4. The function checks if n == 1 or n == 0, but it's not, so it performs another recursive call: 6 * recur_factorial(5).
  5. This continues until we reach the base case of recur_factorial(1) and recur_factorial(0), which both return 1.
  6. Each subsequent call multiplies the result with the current number:
  • 7 * 1 = 7 (from the first recursive call)
  • 6 * 1 = 6 (from the second recursive call)
  • 5 * 1 = 5 (from the third recursive call)
  • ... and so on, until we reach the initial call to recur_factorial(7).
  1. Finally, the function multiplies all the intermediate values together: 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040.

Worked Example

Let's walk through an example to understand how the recursive factorial function works and its execution flow:

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

num = 5
result = recur_factorial(num)
print("Factorial of", num, "is:", result)

When we call recur_factorial(5), the function first checks if n is equal to 1 or 0. Since it's not, it proceeds with the recursive call: 5 * recur_factorial(4). Now, the function checks n again for the base case, and since it's still not 1 or 0, it calls 4 * recur_factorial(3).

This process continues until we reach recur_factorial(2), which checks the base case and returns 2 * 1 = 2. Then, each subsequent call multiplies the result with the current number:

  • 5 * 2 (from the previous step) results in 10
  • 4 * 2 (from the recursive call to recur_factorial(3)) results in 8
  • 3 * 2 (from the recursive call to recur_factorial(2)) results in 6

Finally, when we reach recur_factorial(1), it checks the base case and returns 1. The function then multiplies all the intermediate values together: 10 * 8 * 6 * 1 = 480.

Common Mistakes

Here are some common mistakes to avoid when writing recursive factorial functions in Python:

1. Forgetting the base case

Ensure that you have a proper base case, such as n == 1 or n == 0, in your recursive function. This will prevent infinite loops and ensure that your function eventually returns a value.

2. Incorrect base case

Make sure the base case is correct. For example, if you set the base case to n == 2, the function would not be able to calculate factorials for numbers less than 2.

3. Recursive call placement

Ensure that the recursive call is placed correctly within the function body and that it multiplies the current number with the result of the recursive call, as shown in the example above.

4. Naming conflicts

Avoid naming your function factorial or any other built-in Python functions to prevent name conflicts and ensure smooth execution of your code.

5. Base case order

Ensure that the base case checks for both 0 and 1, as some problems might require calculating factorials for numbers less than or equal to 1.

Practice Questions

  1. Write a Python program to calculate the factorial of 10 using recursion.
  2. Modify the recur_factorial() function to handle negative numbers and return an error message if the input is less than 0.
  3. Create a recursive function that calculates the sum of all integers from 1 to n, where n is passed as an argument.
  4. Write a Python program that finds the factorial of a number entered by the user using recursion.
  5. Modify the recur_factorial() function to calculate the factorial of a given number without using multiplication operators (only addition and subtraction).
  6. Write a recursive function to find the Fibonacci sequence up to n, where n is passed as an argument.
  7. Modify the recur_factorial() function to calculate the factorial of a given number using logarithms instead of multiplication.
  8. Implement a memoization technique to optimize the recursive factorial function and avoid repeated calculations.
  9. Write a Python program that calculates the factorial of a large number (e.g., 100!) using recursion and handle overflow errors if they occur.

FAQ

What happens when we call recur_factorial(0)?

When we call recur_factorial(0), the function immediately returns 1 because it is our base case, and we have defined that the factorial of 0 is 1.

Can we optimize the recursive factorial function in Python to avoid repeated calculations?

Yes, we can use memoization or dynamic programming techniques to store previously calculated values and avoid redundant computations. However, in this specific case, Python's built-in math module already provides an efficient implementation of the factorial function (math.factorial()), so it is not necessary to implement our own recursive version for performance reasons.

Is it possible to write a non-recursive factorial function in Python?

Yes, we can write a non-recursive factorial function using loops. Here's an example:

def fact_iterative(n):
result = 1
for i in range(2, n+1):
result *= i
return result

This function uses a loop to iteratively calculate the factorial of n. It is more efficient than the recursive version when dealing with large numbers.

Python Program to Find Factorial of Number Using Recursion | Python | XQA Learn