Program to find factorial of a number using Python

Introduction

In this section of python coding, we’ll learn how to find the factorial of a given number. A factorial of number n is the multiplication of all the numbers equals and smaller than the given number n.

Program to find factorial of a number using Python

Let’s look into the python coding to find the factorial of the given number.

Program

def fact(f):
    return 1 if (f==1 or f==0) else f * fact(f - 1)
num = 6
n = fact(num)
print("Factorial of {0} is {1}".format(num,n))

Output

Program to find factorial of a number using Python Output

Explanation

In the above code we have created a recursive function fact() which will return the factorial of number. The resultant factorial is saved in variable n and is displayed through print() function.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.