Learn about Python Program to Find Factorial of Number using Recursion in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Python Program to Find Factorial of Number using Recursion
The below code finds the factorial of a Number using Recursion. Initially, the input is taken from the user and the calls the recursive function which returns the factorial value of the given number.
# Python program to find the factorial of a number using recursion
# recursive function
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# 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:
# function call
print('The factorial of',num,'is', recur_factorial(num))
Output:
Enter number: 5
The factorial of 5 is 120
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Factorial Program in Python using While Loop