Python Program to Find Factorial of a Given Number

Factorial of a given number can be computed in many ways in python. In this article we illustrate different approach to calculate factorial of a number.

Method 1: Naive Method (Without Using Any Libraries)


# Python Program to Find Factorial

# Reading number and converting it to integer type
number = int(input('Enter Number: '))

# Set factorial variable to 1
factorial = 1

# Setting loop to find factorial
for i in range(1, number+1):
    factorial = factorial*i

# Displaying factorial value
print('Factorial of %d is %d' %(number, factorial))

Output

Enter Number: 13
Factorial of 13 is 6227020800

Method 2: Using Math Library


# Python Program to Find Factorial

# Importing Math Library
import math

# Reading number and converting it to integer type
number = int(input('Enter Number: '))

# Finding Factorial
factorial = math.factorial(number)

# Displaying factorial value
print('Factorial of %d is %d' %(number, factorial))

Output

Enter Number: 23
Factorial of 23 is 25852016738884976640000