Factorial program in Python helps to calculate the factorial of a number using the Python programming language. Nowadays, Python is one of the most widely used among developers worldwide due to its easy-to-learn and code syntax. It consists of an extensive set of libraries and frameworks to help you perform various operations effectively. Read the complete article to learn how to write a factorial program in Python.
What is the Factorial of a Number?
Factorial of a number ‘n’ is defined as the product of that number with every whole number less than or equal to ‘n’ till 1.
OR
Factorial of a number is a function which multiplies a number by every number less than it or below it till 1. For Example, the factorial of a number 4 is found by multiplying 4,3,2 and 1, i,e. 4! = 4 x 3 x 2 x 1 = 24 which is equal to 24.
Algorithm for Factorial Program in Python
Let us check a recursive algorithm for finding the factorial of a number using Python.
- Step1: Start
- Step 2: Declare a variable n, fact, and i.
- Step 3: Now, read the input number given by the user.
- Step 4: Initialize variable fact with 1 and i with 1.
- Step 5: Repeat until i<=number
fact = fact * i
I = i+1
- Step 6: Print the value of fact
- Step 7: End
Pseudocode for Factorial Program in Python
Let us check the pseudocode for finding the factorial of a number in Python.
Factorial Program in Python |
Read the user input number
Fact = 1 i = 1 WHILE i< = number       Fact = Fact * i       i = i +1 END WHILE PRINT Fact |
Factorial Program in Python: Recursive Approach
Let us check the method to calculate factorial using recursive approach in the table below.
It is one of the most common factorial algorithm which breaks down the factorial into smaller subproblems until it reaches the base case. Check the recursive algorithm for factorial program.
Factorial Program in Python: Recursive Algorithm |
function factorial (n) :Â
       if n == 0             return 1        else:             return n * factorial (n-1) |
Our base case will occur when the value of n becomes 0 then we return 1 otherwise we keep on multiplying by n and calling the function by n-1.
Implementation of Factorial Program in Python using Recursive Approach
Check the code implementation of factorial program in Python in the table below.
Factorial Program in Python: Recursive Algorithm |
class Solution:
|
Factorial Program in Python: Iterative Approach
We can use iterative method to calculate large factorials efficiently. It also avoid the overhead which is caused by function call in recursive approach. It uses loops in place of function calls to calculate the factorial of a number.
Factorial Program in Python: Iterative Algorithm |
function factorial (n):
        result = 1         for i from 2 to n:              result *= i         return result; |
- First we initialize the variable result with 1.
- Now, we will run a loop starting from 2 to the the number itself.
- In each iteration, we keep on multiplying the result variable with the current integer i.
- Finally, after the loops get over we return the result of the factorial n.
With iterative approach we can calculate factorial of a number more efficiently. Let us check the code for implementation of the iterative approach in Python.
Implementation of Factorial Program in Python using Iterative Approach
Let us check the iterative approach implementation using Python in the table below.
Factorial Program in Python: Iterative Algorithm |
def factorial_iterative(n):
    if n < 0:         return None   # Factorial is not defined for negative numbers     result = 1     for i in range(1, n + 1):         result *= i     return result |
Factorial Program in Python: Which Approach is Better?
Let us check the the Difference between Recursive and iterative approach in the table below.
Difference between Recursive and Iterative Approach | |
Recursive Approach | Iterative Approach |
In recursive approach, function calls itself directly or indirectly. | In iterative approach, loops are used to repeatedly execute a set of statements. |
It break down the problem into subproblem and then calculate the factorial. | It uses single loop to find the factorial minimizing the risk of stack overflow. |
This approach is poor for large factorials as it can be time consuming and inefficient because of the repeated function calls. | The iterative approach is efficient for large factorials. |
It consists of overhead caused by recursive function calls. | It do not consists of overhead of recursive function calls. |
It uses more memory. | It uses less memory. |
Learn Python with PW Skills
Enrol in our Decode DSA with Python Course and learn Python Programming with Data Structures and Algorithms all in one course. Learn from industry experts from arrays to advanced sorting techniques, master problem solving with hands on problem exercise. Also, get relevant projects, doubt clearing session, PW Lab access, certification and much more only at pwskills.com.
Factorial Program in Python FAQs
What is a factorial program in Python?
Factorial of a number ‘n’ is defined as the product of that number with every whole number less than or equal to ‘n’ till 1. We can calculate factorial using the Python programming language.
What is the logic of factorial?
In factorial, a function keeps on multiplying a number by every number which is less than it till it becomes less than 1. For Example, the factorial of a number 4 is found by multiplying 4,3,2 and 1, i,e. 4! = 4 x 3 x 2 x 1 = 24 which is equal to 24.
Why is zero factorial 1?
Factorial can only be calculated for positive integers but as there are no positive integer values less than zero. Data cannot be arranged which counts as the possible combination of data arrangement.
What is an example of a factorial?
Consider the factorial of a number 4 is found by multiplying 4,3,2 and 1, i,e. 4! = 4 x 3 x 2 x 1 = 24 which is equal to 24.