Python Program : Factorial of a given number by using recursive
Let’s write a Python program to find the factorial of a given number using recursive method.
A Factorial is the multiplication of a number with every number less than that considered number in a sequential order till 1.
For Instance, Let’s consider a positive integer k. The factorial of k is denoted as k!
k! = k * (k-1) * (k-2) * (k-3) * ……….. * 1
For Example :
7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
A Recursive Function is a function which calls itself again and again till it get its desired result.
IMPLEMENTATION :
def recur_factorial(l):
if l == 1:
return l
else:
return l * recur_factorial(l - 1)
n = 5
if n < 0:
print("Invalid Input")
elif n == 0:
print("The Factorial of 0 is 1")
else:
print("The Factorial of", n, "is", recur_factorial(n))
OUTPUT :
Number : 6 Factorial : 720
Time Complexity : O(n)
Space Complexity : O(n)
Leave a Reply