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

How to Find Factorial of Number Using Recursion in Python?


Factorial of a number is product of all numbers from 1 to that number.

A function is called a recursive function if it calls itself.

In following program factorial() function accepts one argument and keeps calling itself by reducing value by one till it reaches 1.

Example

def factorial(x):
    if x==1:
        return 1
    else:
        return x*factorial(x-1)

f=factorial(5)
print ("factorial of 5 is ",f)

Output

The result is

factorial of 5 is  120