Factorial Program In Python


In this article, we are going to create the factorial program in Python. We will learn the iterative and recursive way to find the nth factorial of a number. Additionally, we will also create a program that tells whether a number is factorial or not.

    Table Of Contents

  1. What is factorial?
  2. Iterative Way to Find Factorial
  3. Recursive Way to Find Factorial
  4. Factorial Using Math Module
  5. Identify whether a Number is Factorial or Not
  6. Conclusion

factorial program in python

What is Factorial?

The factorial of a number is the product of all the numbers from 1 to that number. For example, the factorial of 5 is equal to 5 × 4 × 3 × 2 × 1 = 120.

Mathematically ! (exclamation mark) is used to represent factorial. Factorial of a number n is represented by n!.

1! = 1

2! = 2 × 1 = 2

3! = 3 × 2 × 1 = 6

4! = 4 × 3 × 2 × 1 = 24

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

N! = 1 × 2 × 3 × ... × (N-1) × N = N!

Note: 0! is equal to 1. Factorial of a negative number is not defined.


Iterative Way to Find Factorial

An iterative approach is a simple approach where we use loops to iteratively solve a problem.

Here we will use for loop to iterate through the numbers from 1 to n and multiply them together.

For example, if you want a factorial of n then run a loop from 1 to n and multiply all the numbers together.

# factorial program in python
n = int(input("Enter a number: "))

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

print(f"Factorial of {n} is {fact}")

Output:

Enter a number: 5
Factorial of 5 is 120

Recursive Way to Find Factorial

The recursive approach is a very different way of solving a problem. Here internally we break the problem into smaller sub-problems and solve them recursively.

Recursion is a method of solving a problem by calling a function within itself. Each time the function is executed for different values.

All recursive functions must have a break condition (or base condition). If the break condition is met, the function returns the result. If the break condition is not met, the function calls itself again.

For example, the factorial of 5 is equal to 5 × 4 × 3 × 2 × 1, we can also write it as 5! = 5 × 4! and 4! = 4 × 3! and so on. To generalize N! can be written as N! = N × (N-1)!.

In the case of factorial, the base condition is n = 0 as we know 0! = 1.

Here is the recursive function to find the factorial of a number.

# recursive factorial program in python
n = int(input("Enter a number: "))

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(f"Factorial of {n} is {factorial(n)}")

Output:

Enter a number: 5
Factorial of 5 is 120
factorial recursive

Factorial Using Math Module

The math module is a module in the Python programming language that contains a number of useful mathematical functions.

One of such functions is factorial which is used to find the factorial of a number.

You just need to pass the number to the factorial function and it will return the factorial of that number.

# factorial program in python
import math

num = int(input("Enter a number: "))

fact = math.factorial(num)
print(f"Factorial of {num} is {fact}")

Output:

Enter a number: 5
Factorial of 5 is 120

Identify whether a Number is Factorial or Not

Above we have seen 2 different programs that calculate the Nth factorial number, now let's create a program that tells whether a number is factorial or not.

Here we can apply the reverse approach. Here is the algorithm for this program:

  1. Create a function isFactorial that takes a number as an argument and returns True if the number is factorial or False if the number is not factorial.
  2. First, create a variable that divides the number starting from 1.
  3. Execute a python while loop that runs until the number is greater than 1.
  4. If the number is divisible by the count, then divide the number by the count and increase the count by 1.
  5. If the number is not divisible by the count, then return False.
  6. Finally, return True.
# program to identify a number is factorial or not

def isFactorial(num):
    count = 1
    while num > 1:
        if num % count == 0:
            num = num // count
            count += 1
        else:
            return False
    return True

# checking factorial numbers between 1 to 500
for i in range(1, 500):
    if isFactorial(i):
        print(i, "is a factorial number")

Output:

1 is a factorial number
2 is a factorial number
6 is a factorial number
24 is a factorial number
120 is a factorial number

Conclusion

In this brief guide, we have seen factorial programs in python. Let's recap the points.

  • Factorial of 0 is 1.
  • Factorial for negative numbers does not exist.

  • Factorial is defined as: N! = 1 × 2 × 3 × ... (N-1) × N