Python Program to find factorial of a number
In this tutorial, we will learn how to find the factorial of a number and display it. The factorial of a non-negative number is the product of all the integers from 1 to that number. Factorial of a negative number is not defined and the factorial of 0 is always 1.
For example, Factorial of 7 is: 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040
Formula
The program should take input as follows and return the factorial of that number.
Input- Enter number: 5
Output- Factorial: 120
Input- Enter number: 4
Output- Factorial: 24
There are three different approaches to execute this task in Python:
- Using a recursive function
- Using conditional statements and loops
- Using built-in factorial() function
Let us look at these approaches individually,
Program 1: using recursion
As discussed earlier in this tutorial, recursion is a process in which the function calls itself directly or indirectly in a program. We will define a recursive function for finding the factorial of a number given by the user. Follow the algorithm to understand the working process better.
Algorithm
Step 1 - Define function factorial() to calculate factorial
Step 2 - Check if the entered number is 1 or 0, if true return the number
Step 3 - If false, call the function recursively to calculate factorial of the number minus 1
Step 4 - Return the value of the number multiplied by the factorial of the number minus 1
Step 5 - Print the result
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
def factorial(n):
if (n==1 or n==0):
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter number: "))
x=factorial(num)
print("Factorial of",num,"is",x)
Enter number: 5
Factorial of 5 is 120
Program 2: using conditional statements and loops
In this program, we will use conditional statements like if, elif and else and we will use a looping statement. Conditional statements in Python perform specific tasks depending on whether the condition evaluates as true or false. Whereas loops are used to execute a statement repeatedly. Look at the algorithm mentioned below to understand how we will use these features to find factorial.
Algorithm
Step 1 - Take input from the user
Step 2 - Declare a variable factorial and initialise it to 1
Step 3 - Check if the number is positive or negative
Step 4 - If negative, print "Factorial doest not exist"
Step 5 - For positive numbers, if the number is 0 print factorial as 1
Step 6 - Else run a loop from 1 to the number and multiply each iteration with the value of factorial
Step 7 - Print the result
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
num = int(input("Enter number: "))
factorial = 1
if num < 0:
print("factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Enter number: 5
The factorial of 5 is 120
Program 3: using built-in math function
In the math class of the Python library, there is a built-in function to calculate the factorial of a number. The function factorial() is used to find the factorial of any number.
Algorithm
Step 1 - Import math class
Step 2 - Define a function that returns factorial
Step 3 - Call math.factorial()
Step 4- Print result returned by the function
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach.
import math
def factorial(n):
return(math.factorial(n))
num = int(input("Enter number: "))
print("Factorial of", num, "is",factorial(num))
Enter number: 10
Factorial of 10 is 3628800