Factorial of a Number in Python using Recursion

Factorial of a number in python using recursion | A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

Generally, the factorial of a number can be found using the for loop and while loop. But we can also use the recursion technique to find the factorial of a given integer number. Here the problem of finding n! divide them into a smaller problem as n! = n * (n-1)!

Factorial of a number n is given by 1 * 2 * … * (n-1) * n and it’s denoted by n!
Example Factorial of 5! = 5*4*3*2*1 or 1*2*3*4*5

We can write it as,
5! = 5 * 4!
Similarly, we can write
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1

So, in general, we can say that factorial of a positive integer n is the product of n and factorial of (n-1).

n! = n * (n-1)!

Now, the problem of finding out factorial of (n-1) is similar to that of finding out factorial of n, but it is smaller in size. So, we have defined the solution of the factorial problem in terms of itself. We know that the factorial of 0 is 1 and similarly factorial of 1 is 1. This can act as the terminating condition or the base case.

The base case for finding factorial

factorial(0) = 1
Or,
factorial(1) = 1

General case for finding factorial

factorial(n) = n * factorial(n-1)

Python Program to Find Factorial of Number using Recursion

# Python program to find the factorial of a number using recursion

def recur_factorial(n):  #user-defined function
   if n == 1:
       return n
   else:
       return n*recur_factorial(n-1)

# take input
num = int(input("Enter number: "))

# check number is positive, negative, or zero
if num < 0:
   print('Factorial does not exist for negative numbers')
elif num == 0:
   print('The factorial of 0 is 1')
else:
    # calling function
    print('The factorial of',num,'is', recur_factorial(num))

Output for the different input values:-

Enter number: 4
The factorial of 4 is 24

Enter number: 0
The factorial of 0 is 1

Enter number: 5
The factorial of 5 is 120

In this program, we will be the first defined recursion function.

def recur_factorial(n):  #user-defined function
   if n == 1:
       return n
   else:
       return n*recur_factorial(n-1)

Inputs are scanned using the input() function and stored in variable num.

num = int(input("Enter number: "))

Check number is positive, negative, or zero. Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1. The number is positive then it passed to the recur_factorial() function to compute the factorial of the number and the result will be displayed on the screen.

if num < 0:
   print('Factorial does not exist for negative numbers')
elif num == 0:
   print('The factorial of 0 is 1')
else:
    # calling function
    print('The factorial of',num,'is', recur_factorial(num))

In the above program, find the factorial of a number in python using for loop and while loop but many different ways to find factorial of a number in python.

Similar Program to find factorial in Python:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *