Computer >> Computer tutorials >  >> Programming >> Python

How to write recursive Python Function to find factorial?


The following code calculates the factorial for n = 6 and n = 15

Example

def factorial(n):
    if n == 1:
      return 1
    else:
      res = n * factorial(n-1)
    return res
print ("factorial(6) = %d"  %factorial(6))
print ("factorial(15) = %d"  %factorial(15))

Output

We get the output

C:/Users/TutorialsPoint1/~.py
factorial(6) = 720
factorial(15) = 1307674368000