Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to write recursive Python Function to find factorial?

How do I write a recursive python function to find factorial of a natural number 6 and a natural number 15?


1 Answer
Rajendra Dharmkar

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

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))

We get the output

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.