Find Factorial of Number in Python Using Recursion

A factorial of a number is the product of all positive integers less than or equal to that number. In mathematics, the factorial of a non-negative integer n is represented as n! For example, the factorial of 5 is 5! = 5 x 4 x 3 x 2 x 1 = 120. In this tutorial, we will learn how to find the factorial of a number in Python using recursion.

What is Recursion?

Recursion is a technique in computer programming where a function calls itself until it reaches a specific stopping condition. This allows us to solve problems by breaking them down into smaller sub-problems and solving each sub-problem individually.

Python Program to Find Factorial of Number Using Recursion

# Find Factorial of Number in Python Using Recursion
def recur_factorial(n):
   if n == 1:
   	return n
   else:
   	return n*recur_factorial(n-1)

# we are taking a number from user as input
# entered value will be converted to int from string
num = int(input("Enter a number: "))

# check if the number is negative
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
	# number is passed to the recur_factorial() function
   print("The factorial of", num, "is", recur_factorial(num))

The code given above calculates the factorial of a number in Python using recursion. Here’s how the code works:

  1. recur_factorial(n): This function takes an integer n as an argument and calculates the factorial of that number using recursion.
  2. if n == 1:: If the number entered by the user is 1, the function returns 1 as the factorial of 1 is 1.
  3. else:: If the number is not 1, the function returns n multiplied by the factorial of n-1. This is where the recursion takes place. The function calls itself with the argument n-1, which is one less than the original number until n becomes 1.
  4. num = int(input(“Enter a number: “)): This line of code takes input from the user and converts it to an integer.
  5. if num < 0:: If the number entered by the user is negative, an error message is displayed stating that the factorial of a negative number does not exist.
  6. “elif“ num == 0:: If the number entered by the user is 0, the code displays the message that the factorial of 0 is 1.
  7. else:: If the number entered by the user is positive, the function recur_factorial(num) is called and the result is displayed as the factorial of the number.

Output:

Enter a number: 5
The factorial of 5 is 120

Advantages of Using Recursion

  1. Simplicity: Recursion can make the code much simpler and easier to understand, especially for mathematical problems.
  2. Reusability: Recursive functions can be used again and again for solving similar problems, which increases code reusability.
  3. Abstraction: Recursion allows us to hide the implementation details and work on the abstract level, making the code more abstract and maintainable.

Limitations of Using Recursion

  1. Overhead: Recursive functions have overhead due to the function call overhead and the overhead of maintaining the call stack.
  2. Memory usage: Recursive functions use a lot of memory because each call creates a new stack frame, which consumes memory.
  3. Stack overflow: If the recursion goes too deep, it can cause a stack overflow error. This means that the call stack has exceeded its maximum size, and the program terminates.

Tips for Using Recursion

  1. Always have a base case: A base case is a condition that stops the recursion. Without a base case, the recursion will go on forever and cause a stack overflow error.
  2. Use the right data structures: Recursion works well with recursive data structures like trees and links lists.
  3. Avoid deep recursion: Deep recursion can cause a stack overflow error, so it’s important to keep the recursion depth under control.

Conclusion

In this tutorial, we learned about finding the factorial of a number in Python using recursion. We saw the advantages and limitations of using recursion, and also learned some tips for using recursion effectively. Recursion is a powerful technique that can simplify code and make it easier to understand, but it’s important to use it correctly to avoid common pitfalls like stack overflow errors.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply