The factorial of a number is the multiplication of that number by a positive number less than it till it gets to 1. This concept has various applications in mathematics, business, and engineering fields, making it an important computation. Although you can press your calculator to compute the factorial of any number, it would be interesting to find out hard code a program to do this. After reading this tutorial, you will discover how to write a factorial program in python. As a matter of fact, we will discuss the 3 common approaches that can be used when solving this problem. Let’s dive in.
Table of Contents
Factorial in Python
The factorial of a positive integer is regarded as the multiplication of all the integers smaller than the number (until it gets to 1) and the number itself. For example, looking at the number 7, the factorial of 5 is 7*6*5*4*3*2*1 = 5040.
You may however be wondering what the factorial of 0 is. Well, the factorial of 0 is 1.
Before going on to write a factorial program in python, you need to have basic knowledge on the python if…else statements and the python for loop statement.
When dealing with writing a factorial program in python, there are 3 different methods by which we can write the program and they are:
- Using recursion methods
- With for loops
- Using the built-in maths function
These 3 methods are explained below using python codes.
Factorial Program in Python Using Recursion Method
To write this code, the concept of python recursion should be known. A sample of the code is written below
def recursion_factorial(n):
"""This fucntion computes the factorial of a number"""
if n == 1:
return n
else:
return n*recursion_factorial(n-1)
#input soome number
number = int(input('Enter a number: '))
#check if the number is a negative number
if number <0:
print("Invalid input. Please enter a positive integer")
elif number == 0:
print(f"The factorial of {number} is 1")
else:
print(f"The factorial of {number} is {recursion_factorial(number)}")
Output:
Enter a number: 7
The factorial of 7 is 5040
Using For Loops
With for loop, the code is relatively simpler and easier to understand.
#A python code for finding the factorial of a number using for loop
#input the number
number = int(input("Enter a number: "))
#check if the number is a negative number
if number <0:
print("Invalid input. Please enter a positive integer")
else:
#set the factorial to 1
factorial = 1
#use the for loop
for integer in range(1, number + 1):
factorial = factorial * integer
print(f"The factorial of {number} is {factorial}")
Output:
Enter a number: 5
The factorial of 5 is 120
Factorial Program in Python using the math Module
This is perhaps the easiest method. The math module in python has a method that calculates the factorial of a given number – factorial() method. To do this, we first import the module. Thereafter, we called the factorial() method and pass the number as an argument.
#First, we import the math module function
import math
#we input the number
number = int(input("Enter a number: "))
factorial_number = math.factorial(number)
print(f"The factorial of {number} is {factorial_number}")
Output:
Enter a number: 6
The factorial of 6 is 720