python

Calculate the factorial of a number using python code

A recursive function is the best way to calculate the factorial of a number using Python.

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

num = 4
print("The factorial of", num, "is", factorial(num))
Was this helpful?