Python Program to Find Factorial of a Given Number

This python programming example explains different methods to calculate factorial of a given positive integer number.

Definition of Factorial: In mathematics, the factorial of a positive integer number n, denoted by n!, is the product of all positive integers less than or equal to n.

Factorial Examples: 5! = 5*4*3*2*1 = 120, 0! = 1, 3! = 3*2*1 = 6, ... etc

Python Source Code (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

Python Source Code (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