Python Program to Find Factorial of Number Using Recursion – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Find Factorial of Number Using Recursion.

Code

# Python Program to Find Factorial of Number Using Recursion

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

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:
    print("The factorial of", num, "is", factorial_recur(num))

Output

Enter a number -> 5
The factorial of 5 is 120
Enter a number -> 10
The factorial of 10 is 3628800
Enter a number -> 7
The factorial of 7 is 5040
  • Here we have used the concept of recursion to calculate the factorial of a number entered by the user.
  • For example, the factorial of 5 can be calculated as follow:

5! = 5X4!
4! = 4X3!
3! = 3X2!
2! = 2X1!

Check out our other python programming examples

Leave a Comment

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