What is factorial?
In Python, any other programming language or in common term the factorial of a number is the product of all the integers from one to that number. Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to one, then factorial of n is,
(n!) = 1*2*3*4....*n
Also, the factorial value of zero is equal to one and the factorial values for negative integers are not defined.
Examples:
- 4! = 4 × 3 × 2 = 24
- 7! = 7 × 6 × 5 × 4 × 3 × 2 = 5040
- and factorial of one is one
Calculating From the Previous Value
We can easily calculate a factorial from the previous one:
As a table:
n | n! | ||
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 × 1 | = 2 × 1! | = 2 |
3 | 3 × 2 × 1 | = 3 × 2! | = 6 |
4 | 4 × 3 × 2 × 1 | = 4 × 3! | = 24 |
5 | 5 × 4 × 3 × 2 × 1 | = 5 × 4! | = 120 |
6 | etc | etc |
- To work out 7! multiply 7 by 720 to get 5040
- And so on
Example: 8! equals 40320. Try to calculate 9!
9! = 9 × 8!
9! = 9 × 40320 = 3,62880
So the rule is:
n! = n × (n−1)!
Which says
“the factorial of any number is that number times the factorial of (that number minus one)“
Finding factorial of a number in Python using Iteration
(i) Factorial of a Number using for Loop
Program
num = int(input("Enter the Number :"))
fact = 1
if num < 0:
print("Factorial of negative number is not defined")
else:
for i in range(1,num+1):
fact *= i
print("Factorial of {} is {}".format(num, fact))
Output
Enter the Number :7
Factorial of 7 is 5040
In this python program first, we are taking input from the keyboard using the input()
function then we have a conditional statement for negative inputs since factorial of a negative number doesn’t exist.
Then we have a for loop in range 1 to the number itself inside the loop we are simply iterating over loop variable i
and multiplying it with the fact
variable defined above. In the end, we are simply printing out the result using string formatting.
(ii) Factorial of a Number using While Loop
def factorialUsingWhileLoop(n):
fact = 1
while(n>1):
fact = fact*n
n = n - 1
print('Factorial is %d'%(fact))
if __name__== "__main__":
factorialUsingWhileLoop(4)
Similar to the above program, we can use one ‘while‘ loop to find out the factorial. The process is the same. The only difference is that we are using one ‘while‘ loop instead of a ‘for loop‘.
‘factorialUsingWhileLoop‘ method is used to find out the factorial using a while loop. Similar to the above program, the variable ‘fact‘ is used to hold the final factorial value. The while loop will run until the value of ‘n’ is greater than ‘one’. On each iteration of the loop, we are decreasing the value of ‘n‘ by ‘one‘. This loop will exit when the value of ‘n‘ will be ‘0‘. We are printing the factorial value when it ends.
Read more: What is Null in Python
Finding factorial of a number in Python using Recursion
Recursion means a method calling itself until some condition is met. A method which calls itself is called a recursive method. A recursive method should have a condition which must cause it to return else it will keep on calling itself infinitely resulting in memory overflow.
Calculating the factorial of a number using the recursive method should work on the following algorithm.
*. Create a method which accepts one argument.
*. Check if the value of the argument is one, then return one from this method. This will be the terminating condition of the recursive method.
*. Return the product of the argument and the return value of this method called (argument – one).
Code follows
# recursive method which returns the factorial of a number
def factorial_recursion(number):
# check if number is 1
if number == 1:
# return 1 from the method
return 1
return number * factorial_recursion(number-1)
# factorial = factorial_recursion(5)
# read number from keyboard
number = int(input("Enter a number\n"))
# find factorial
factorial = fact(number)
print("Factorial is "+str(factorial))
The method will keep on calling itself and return the product of the argument supplied to it with one less than the argument. When the argument value is one, it will return one. This way, the recursive method will return the product of all the numbers starting from the argument till one.
The output of the above program will be
Enter a number
6
Factorial is 720
Python Program to find Factorial of a Number using Functions
This python factorial program is the same as the 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:
Please enter any Number to find factorial : 6
The factorial of 6 = 720
Built-in solution for computing factorial of a number in Python
from math import factorial
num = int(input("Enter the Number :"))
fact = factorial(num)
if num < 0:
print("Factorial of negative number is not defined")
else:
print("Factorial of {} is {}".format(num, fact))
Output:
Enter the Number :5
Factorial of 5 is 120
Conclusion :
In this tutorial, we have learned how to find out the factorial of a number in python. We have learned how to calculate the factorial using four different methods. Try to run the examples shown above and drop one comment below if you have any queries.