Python | Find Factorial of a Number

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.

Finding Factorial of a Number using Recursion

  1. Define number whose factorial to be calculated as num = 6
  2. Define a Python Function factorial(num) which recursively multiply num with num-1, num-2 up to 1 and returns final number
  3. 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

num = 6                 # Define Number whose Factorial to be calculated
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

  1. Define Number whose Factorial need to be calculates as n = some_number
  2. Define a Python Function factorial(n) which iteratively multiply n with n-1, n-2 up to 1 and returns final number
  3. 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
  4. 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

n = 9       			 # Define Number whose Factorial to be calculated

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

  1. Import Python’s math module into code using import math statement
  2. Define number whose factorial to be found as num = some_number
  3. 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

num = 8                # Define Number whose Factorial to be Calculated

def factorial(num):
	return(math.factorial(num))

print("Factorial of", num, "is => ",factorial(num))

Output of Above Code

Factorial of 8 is =>  40320

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts