Factorial is denoted with symbol (!). The Factorial of any number can be, product of all the numbers that are less than or equal to that number and greater than 0.

n! = n * (n-1) * (n -2) * …….* 1

For example, Factorial of 5 is represented as

5! = 5 *4 * 3 * 2 * 1 = 120

In this article, we will show you, How to write a Python program to find Factorial of a Number using For Loop, While Loop, Functions, and Recursion.

## Python Program to find Factorial of a Number using Math function

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

# Python Program to find Factorial of a Number import math number = int(input(" Please enter any Number to find factorial : ")) fact = math.factorial(number) print("The factorial of %d = %d" %(number, fact))

**OUTPUT**

## Python Program to find Factorial of a Number using For Loop

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

# Python Program to find Factorial of a Number 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))

**OUTPUT**

**ANALYSIS**

User entered integer in the above example is 4

**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 will become 5. So, For loop will be Terminated.

## Python Program to find Factorial of a Number using While Loop

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

# Python Program to find Factorial of a Number 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))

**OUTPUT**

## Python Program to find Factorial of a Number using Functions

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

# Python Program to find Factorial of a Number 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))

**OUTPUT**

## Python Program to find Factorial of a Number using Recursion

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

# Python Program to find Factorial of a Number def factorial(num): if((num == 0) or (num == 1)): return 1 else: return num * factorial(num - 1) number = int(input(" Please enter any Number to find factorial : ")) fact = factorial(number) print("The factorial of %d = %d" %(number, fact))

**OUTPUT**

**ANALYSIS:**

Within the user defined function,

If Else Statement will check whether the Number is Equal to 0 or 1

- If the condition is TRUE then the function will return 1.
- If the condition is False, function will return Number * (Number -1) recursively.

User Entered Value = 6

Factorial = Number * Factorial (Number -1);

= 6 * Calculate_Factorial (5)

= 6 * 5 * Calculate_Factorial (4)

= 6 * 5 * 4 * Calculate_Factorial (3)

= 6 * 5 * 4 * 3 * Calculate_Factorial (2)

= 6 * 5 * 4 * 3 * 2 * Calculate_Factorial (1)

= 6 * 5 * 4 * 3 * 2

= 720