Python Program to find Factorial of a Number

Write a Python program to find the Factorial of a Number using For Loop, While Loop, Functions, and Recursion. The Factorial of a number is the product of all numbers less than or equal to that number & greater than 0. n! = n * (n-1) * (n -2) * …….* 1.

Python Program to find Factorial of a Number using math function

It is denoted with the exclamation mark (!), and in this Python program code, we are using the built-in math module factorial function on the number to find it.

import math 

a = int(input(" Please enter any Integer : "))

ft = math.factorial(a)
print("The Result of %d  = %d" %(a, ft))
Please enter any Integer : 5
The Result of 4 = 120

In this example, we use the math prod() function that accepts the iteration value and finds the product of eachvalue.

import math

num = int(input("Enter any Number : "))

if num < 0:
    print("Please Enter Positive Integer Only.")
elif num == 0:
    print("The Fact of 0 = 1.")
else:
    fact = math.prod(range(1, num + 1))
    print(f"The Fact of {num} is {fact}.")
Enter any Number : -6
Please Enter Positive Integer Only.

Enter any Number : 7
The Fact of 7 is 5040.

Python Program to find Factorial of a Number using For Loop

This code allows the user to enter any integer. Using this given value, this Python program finds the Factorial of a number using For Loop.

number = int(input(" Please enter any Number : "))
fact = 1

for i in range(1, number + 1):
    fact = fact * i
print("The factorial of %d  = %d" %(number, fact))
Python Program to find Factorial of a Number 2

The user entered integer in the above program example is 4. Please refer to math functions, For Loop, While Loop, and Functions articles in Python.

First Iteration
i = 1, Fact = 1 and number = 5

Fact = Fact * i;
Fact = 1 * 1 = 1

Second Iteration
i = 2, Fact = 1 and Number = 5
Fact = 1 * 2 = 2

Third Iteration
i = 3, Fact = 2 and Number = 5
Fact = 2 * 3 = 6

Fourth Iteration
i = 4, Fact = 6 and Number = 5
Fact = 6 * 4 = 24

Next, i become 5. So, For loop Terminated.

The above code for the factorial program in Python returns 1 for the negative values. So, we need the If else statement to print a message for negative input.

num = int(input("Enter any Number : "))
fact = 1

if num < 0:
    print("Please Enter Positive Integer Only.")
else:
    for i in range(1, num + 1):
        fact = fact * i
    print("The factorial of %d  = %d" %(num, fact))
Enter any Number : -9
Please Enter Positive Integer Only.

Enter any Number : 6
The factorial of 6  = 720

Python Program to find Factorial of a Number using While Loop

In this program, we just replaced the for loop with While Loop to find the factorial of a number.

value = int(input(" Please enter any Value : "))
fact = 1
i = 1

while(i <= value):
    fact = fact * i
    i = i + 1

print("The Result of %d  = %d" %(value, fact))
Please enter any Value : 8
The Result of 8 = 40320

output 2

Please enter any Value : 9
The Result of 9 = 362880

Python Program to find Factorial of a Number using Functions

This code is the same as the first example. However, we separated the factorial logic using the Functions.

def calculating(num):
    faco = 1

    for i in range(1, num + 1):
        faco = faco * i

    return faco


val = int(input(" Please enter any Value : "))

faco = calculating(val)
print("The Result of %d  = %d" %(val, faco))
Please enter any Value : 5
The Result of 5 = 120

output 2

Please enter any Value : 6
The Result of 7 = 720

Python Program for Factorial of a Number using Recursion

This code passes the user entered value to the Function. Within this recursive function, this example program finds the factorial of a number using the recursive function or recursively.

def factFind(num):
    if num < 0:
        print("Please Enter Positive Integer Only.")
    elif num == 0:
        return 1
    else:
        return num * factFind(num - 1)

num = int(input("Enter any Num : "))
print(f"Fact of {num} is {factFind(num)}.")
Enter any Number : 6
Fact of 6 is 720.

output 2

Enter any Number : -9
Please Enter Positive Integer Only.
Fact of -9 is None.

Within the user-defined function of this program, the If Else Statement checks whether the integer is Equal to 0 or 1. If the condition is TRUE, then the function returns 1. If the condition is False, the function returns Num * (Num -1) recursively.

User Entered Value = 6.

Fac = num * factFind (num -1);
= 6 * factFind (5)
= 6 * 5 * factFind (4)
means 6 * 5 * 4 * factFind (3)
= 6 * 5 * 4 * 3 * factFind (2)
= 6 * 5 * 4 * 3 * 2 * factFind(1)
Fac = 6 * 5 * 4 * 3 * 2
= 720

Python Program for Factorial of a Number using List comprehension

num = int(input("Enter any Number : "))

if num < 0:
    print("Please Enter Positive Integer Only.")
elif num == 0:
    print("The Fact of 0 = 1.")
else:
    fact = 1
    [fact := fact * i for i in range(1, num + 1)]
    print(f"The Fact of {num} is {fact}.")
Enter any Number : 5
The Fact of 5 is 120.

Python Program for Factorial of a Number using lamda reduce

This example uses the lambda function and reduce function.

from functools import reduce

num = int(input("Enter any Number : "))

if num < 0:
    print("Please Enter Positive Integer Only.")
elif num == 0:
    print("The Fact of 0 = 1.")
else:
    fact = reduce(lambda a, b: a * b, range(1, num + 1), 1)
    print(f"The Fact of {num} is {fact}.")
Enter any Num = 9
The fact of 9  = 362880

This example is same as the above, however it uses the lambda recusive function.

from functools import reduce

def factorial(num):
    return reduce(lambda a, b: a * b, range(1, num + 1), 1)

num = int(input("Enter any Number : "))

if num < 0:
    print("Please Enter Positive Integer Only.")
elif num == 0:
    print("The Fact of 0 = 1.")
else:
    print(f"The Fact of {num} is {factorial(num)}.")
Enter any Number : 0
The Fact of 0 = 1.

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.