Online Tutorials & Training Materials | STechies.com
Register Login

Python Program to find factorial of a number

|| 0

Python Program to find factorial of a number
Stechies

What is Factorial?

Factorial of any positive number (n) is a product of all number which is less than or equal to (n) but greater than 0.

for example Factorial of 4 = 4*3*2*1

Note: Factorial is represented by exclamatory mark (!).

Mathematical formula for Factorial

n ! = n ( n - 1)( n - 2)( n - 3)( n - 4)(n - 5)... (5)(4)(3)(2)(1)

where n should be greater than 0. There is no factorial defined for negative numbers whereas factorial of 0! is 1.

In this tutorial, you will learn how to write a python program to find factorial of any non-negative integer which is greater than 0.

1) Python Program to calculate Factorial using for loop:

#Python program to find factorial of a number using for loop

#Taking input of Integer from user
n = int(input("Enter a number : "))
#Declaring and Initilizing factorial
factorial = 1

#check if number is negative
#Factoral can't be find of negative number
if (n < 0 ):
    #If Yes,Then diplay the error message
    print("Error! You have entered negative value")
else:
    #If No,
    #For Loop for iteration i = 1 to number itself
    #range opts n-1 so we add 1 in n-1
    #For example (if user pass 24 in range it will go till 23)
    for i in range(1,n+1):
        #Capture factorial value in factorial vaiable
        #Formula is n! => n*(n-1)*(n-2)*(n-3).......*3*2*1
        factorial *= i
    #Print the Factorial of entered number
    print("Factorial : ", factorial) 

OUTPUT :

Enter a number : 10
Factorial :  3628800

2) Python Program to calculate Factorial using a while loop

#Python program to find factorial of a number using While Loop

#Taking input of Integer from user
n = int(input("Enter a number : "))
#Declaring and Initilizing variables
i = 1
factorial = 1

#check if number negative
#Factoral can't be find of negative number
if (n < 0 ):
    #If Yes,Than diplay the error message
    print("Error! You have entered negative value")
else:
    #If No
    #While Loop for iteration i = 1 to number itself
    while( i <= n) :
        #Capture factorial value in factorial vaiable
        #Formula is n! => n*(n-1)*(n-2)*(n-3).......*3*2*1
        factorial = factorial * i
        #Increase the value of value of `i` on every iteration
        i = i + 1
    #Print the Factorial of entered number
    print("Factorial : ", factorial)

OUTPUT :

Enter a number: 12
Factorial: 479001600 

Calculate Factorial using Python factorial() function

Python math library contains a factorial() function which calculates the factorial of the input value and returns its output value.

Syntax of factorial()

math.factorial(n)

where n is any positive integer

3) Calculate factorial using Python math.factorial function 

#Python program to find factorial of a number using a library function

#importing the math library
import math

#Taking input of Integer from user
n = int(input("Enter a number : "))

#check if number negative
#Factoral can't be find of negative number
if (n < 0 ):
    #If Yes,Than diplay the error message
    print("Error! You have entered negative value")
else:
    #If No
    #factorial method of math library
    factorial = math.factorial(n)
    #Print the Factorial of entered number
    print("Factorial : ",factorial) 

OUTPUT :

Enter a number: 10
Factorial: 3628800 

Output 2:

Enter a number: -1
Error! You have entered negative value