Factorial of a non-negative number is defined as multiplication of all natural numbers smaller than it upto 1. For example – Factorial of -19 is not defined as its negative while factorial of 4 will be defined as 4 * 3 * 2 * 1 = 24. Quite simple just multiply n(n-1)(n-2)(n-3)(n-4)………..upto 1 and you would get factorial of number n.
In this article, I’ll discuss algorithmic ways for implementing Finding Factorial of a Number using Python Programming Language. There can be three different approaches which can be used => Recursion, Iteration and Using Python’s built-in methods.
Let’s discuss each one of this algorithmic approach and put these together as Python Code for finding Factorial of Number.
Table of Contents
Finding Factorial of a Number using Recursion
- Define number whose factorial to be calculated as num = 6
- Define a Python Function factorial(num) which recursively multiply num with num-1, num-2 up to 1 and returns final number
- Final Number retuned by Function factorial(num) will be Factorial of number num
Let’s put together all of these 3 steps as Python Code for Finding Factorial of a Number using Recursion.
# Find Factorial of a number using Recursion
# Define Number whose Factorial to be calculated
num = 6
def factorial(num):
# single line to find factorial
return 1 if (num==1 or num==0) else num * factorial(num - 1);
print("Factorial of",num,"is", factorial(num))
Output of Above Code
Factorial of 6 is 720
Finding Factorial of a Number using Iterations
- Define Number whose Factorial need to be calculates as n = some_number
- Define a Python Function factorial(n) which iteratively multiply n with n-1, n-2 up to 1 and returns final number
- Firstly Python Function factorial(n) checks if n is less than 0, if so then return 0 otherwise checks if n is 0/1 if so then return 1
- Otherwise if n is neither 0/1 or less than 0 then use fact = 1 as a counter and iteratively numltiply it with n, n-1, n-2 up to 1. And then return fact, here fact will be Factorial of number n
Let’s put together all of these 4 steps as Python Code for finding factorial of a Number using Iterations.
# Python 3 program for finding Factorial using Recursion
# Define Number whose Factorial to be calculated
n = 9
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
print("Factorial of",n,"is =>",factorial(n))
Output of Above Code
Factorial of 9 is => 362880
Finding Factorial of a Number using Built-in Functions
- Import Python’s math module into code using import math statement
- Define number whose factorial to be found as num = some_number
- Define Python Function factorial(num) which takes in num and using factorial function from Math module calculates Factorial of num, then return it
# Pytho Program to Find factorial of a number using Built-in Functions
# Import Python's Math Module
import math
# Define Number whose Factorial to be Calculated
num = 8
def factorial(num):
return(math.factorial(num))
print("Factorial of", num, "is => ",factorial(num))
Output of Above Code
Factorial of 8 is => 40320
No Comments
Leave a comment Cancel