Python | Find factorial of a given number (2 different ways)

Here, we are going to actualize rationale to discover factorial of a given number in Python,

there are two techniques that we are going to utilize 1) utilizing circle and 2) utilizing recursion strategy.

Given a number and we need to locate its factorial in Python.


    Num = 4

    Factorial of 4 is: 24

To locate the factorial, reality() work is written in the program. This capacity will take a number (num) as

1) Method 1: Using loop

# Code to find factorial on num

# number
num = 4

# 'fact' - variable to store factorial 
fact =1 

# run loop from 1 to num
# multiply the numbers from 1 to num
# and, assign it to fact variable 

for i in range (1,num+1) :
	fact = fact*i 

# print the factorial
print "Factorial of {0} is: {1} ".format (num, fact)


   Factorial of 4 is: 24 

2) Method 2: by creating a function using recursion method


# function to calculate the factorial 
def fact (n):
	if n == 0:
		return 1 
	return n * fact (n - 1)

# Main code 
num = 4

# Factorial
print "Factorial of {0} is: {1} ".format (num, fact(num))


 Factorial of 4 is: 24 

contention and return the factorial of the number.

Leave a Comment

error: Alert: Content is protected!!