Python Examples

Python Program to Find Factorial of Number Using Recursion

Factorial recursion is a method in which a function directly or indirectly calls itself. In mathematics, Factorial means the product of all the positive integers from 1 to that number. An exclamation mark is used after the integer to show that it’s a factorial.  

For example, factorial eight is 8! So, it means multiplication of all the integers from 8 to 1 that equals 40320. Factorial of zero is 1. We can find a factorial of a number using python. There are various ways of finding; The Factorial of a number in python. Among which recursion is the most commonly used.

 

Factorial recursion

                                               Source: Self

Factorial Function in Python

Source Code: Python Program to find factorial of a number using recursive function

                    

def recursive_factorial(n):
  if n == 1:
     return n
  else:
     return n*recursive_factorial(n-1)     #function calling itself

#taking input from the user
number = int(input("User Input : "))
print("The factorial of", number, "is", recursive_factorial(number))

Output :

                    

User Input: 4
The factorial of 4 is 24

Source code: Displaying an error message if a user inputs a negative integer

                    

def recursive_factorial(n):
    if n == 1:
        return n
    else:
        return n*recursive_factorial(n-1) 

#taking input from the user
number = int(input("User input : "))

# If the user input negative integer
if number < 0:
  print("Bad Input")
else:
  print("The factorial of", number, "is", recursive_factorial(number))

Output:

Source code: Factorial of zero is 1.

                    

def recursive_factorial(n):
  if n == 1:
       return n
  else:
       return n*recursive_factorial(n-1)

#taking input from the user
number = int(input("User Input : ")

# If the user input negative integer
if number < 0:
    print("Bad Input")

# If the user inputs 0
elif number == 0:
    print("The factorial of 0 is 1")
else:
    print("The factorial of", number, "is", recursive_factorial(number))

Output:

                    

User Input : 0
The factorial of 0 is 1

FAQs on Factorial Recursion

What is Factorial recursion?

Factorial of a positive integer is the multiplication of all integers from that number to 1.

Factorial is represented by an exclamation mark.

If the user inputs a number let’s assume the number is n, so n factorial is n! = n * (n-1)!

For example:

factorial of 3 is

3! = 3 * 2 * 1. which equals 6.

There is no factorial output for negative integers

So, the factorial of 0 is 1

How does Factorial recursion work?

Factorial of a number is the multiplication of all the integers from that number to 1.

 

Factorial recursion

                                             Source: Self

The input value (positive integer) is passed; as a parameter to the factorial function.

So, Whenever the function runs, It multiplies the value of a parameter and recursively calls the function itself while decrementing the parameter value by 1.

Once the value becomes 0 the function returns 1 (as the factorial of 0 is 1) and returns the multiplier value that is the factorial of an inputted number.

How do you find the Recursive Factorial?

Factorial recursion is a function that is defined in such a way that it calls itself. Until it returns the factorial of the number passed as a parameter to the function.

Formula to calculate Factorial recursion. 

n!=n * (n-1)!  also [n!=1 if n=0] 

factorial of 0 is 1

There is no factorial output for negative integers.

What is recursion? Write a program for factorial calculation.

Recursion is a concept where you can loop through data to get a result. A problematic function can be broken down, into smaller sub-parts by using recursion.

Program for factorial calculation:

 

                    

def recursive_factorial(n): 

 if n == 1:

       return n 

else:

       return n*recursive_factorial(n-1)

num = int(input("User Input :"))

print("The factorial of", num, "is", recursive_factorial(num))

Output 
User Input :5

The factorial of 5 is 120

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.