Subscribe Us

Find factorial of a Given Number In Python

Factorial in Python

Factorial

Python Program To Find Factorial of a Given number

Factorial of n is the product of all positive descending integers.
Factorial of n is denoted by n!.
For example:
5! = 5*4*3*2*1 = 120   3! = 3*2*1 = 6   Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).

Source Code
// Python Program To Find Factorial of a Given number ?
num=int(input("Enter a Number: "))
fact=1
for i in range(num,0,-1):
    fact=fact*i
print("Factorial of Given Number",num,"is",fact)

Sample Input :
Enter a Number : 5
Sample Output:
Factorial of a Given Number 5 is : 120

Sample Input :
Enter a Number: 6
Sample Output:
Factorial of a Given Number 6 is : 720

Using User Define Function
def toFindFact(N):
    fact=1    
    for i in range(N,0,-1):
        fact*=i
    return fact

num=int(input("Enter a Number: "))
fact = toFindFact(num)
print("Factorial of Given Number ",num,"is",fact)

Sample Input :
Enter a Number : 5
Sample Output:
Factorial of a Given Number 5 is : 120

Sample Input :
Enter a Number: 6
Sample Output:
Factorial of a Given Number 6 is : 720

Using Recursion or Recursive Function
def toFindFact(N):
    if N==0:
        return 1
    return N*toFindFact(N-1)
 
num=int(input("Enter a Number: "))
fact = toFindFact(num)
print("Factorial of Given Number",num,"is",fact)

Sample Input :
Enter a Number : 5
Sample Output:
Factorial of a Given Number 5 is : 120

Sample Input :
Enter a Number: 6
Sample Output:
Factorial of a Given Number 6 is : 720