Python Program to find Factorial of a Number

Write a Python program to Find Factorial of a Number using For Loop, While Loop, Functions, and Recursion. The Python Factorial denoted with the symbol (!). The Python Factorial of 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

In this Python factorial of a number program, we are using the built-in math function called factorial.

import math 

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

fact = math.factorial(number)
print("The fact of %d  = %d" %(number, fact))

Python math factorial function output

Please enter any Number : 5
The fact of 4 = 120

Python Program to find Factorial of a Number using For Loop

This python program for the factorial of a number allows the user to enter any integer value. Using this value, it finds the Factors of a number using For Loop.

number = int(input(" Please enter any Number to find factorial : "))
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

User entered integer in the above python program example is 4. Please refer to math functions, factorial, For Loop, While Loop, and Functions in Python.

Python factorial program 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.

Python Program to find Factorial of a Number using While Loop

In this python factorial program, we just replaced the for loop with While Loop

number = int(input(" Please enter any Number to find factorial : "))
fact = 1
i = 1

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

print("The factorial of %d  = %d" %(number, fact))
Please enter any Number to find factorial : 8
The factorial of 8 = 40320

output 2

Please enter any Number to find factorial : 9
The factorial of 9 = 362880

Python Program to find Factorial of a Number using Functions

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

def factorial(num):
    fact = 1

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

    return fact


number = int(input(" Please enter any Number to find factorial : "))

facto = factorial(number)
print("The factorial of %d  = %d" %(number, facto))
Please enter any Number to find factorial : 5
The factorial of 5 = 120

output 2

Please enter any Number to find factorial : 6
The factorial of 7 = 720

Python Program to find Factorial of a Number using Recursion

This Python program pass the user entered value to the Function. Within this function, this Python program finds the Factorial of a number Recursively.

def factFind(num):
    if((num == 0) or (num == 1)):
        return 1
    else:
        return num * factFind(num - 1)


number = int(input(" Please enter any Num : "))

fact = factFind(number)
print("The fact of %d  = %d" %(number, fact))
Please enter any Num : 6
The fact of 6 = 720

output 2

Please enter any Num : 4
The fact of 4 = 24

Within the user-defined function of this python factorial program, If Else Statement check whether the Number 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)
= 6 * 5 * 4 * factFind (3)
= 6 * 5 * 4 * 3 * factFind (2)
= 6 * 5 * 4 * 3 * 2 * factFind(1)
= 6 * 5 * 4 * 3 * 2
= 720