Site icon T4Tutorials.com

Factorial of a Number in Python

Write a Program in Python to find the Factorial of a Number

GivenNumber = int(input("Enter a Number: "))    
factorial = 1    
if GivenNumber < 0:    
   print(" Sorry! The Factorial does not exist for negative Numbers")    
elif GivenNumber == 0:    
   print("Please Note that The factorial of 0 is 1")    
else:    
   for loopVariable in range(1,GivenNumber + 1):    
       factorial = factorial*loopVariable    
   print("The factorial of",GivenNumber,"is",factorial)

Output

Please Enter a Number: 4
The factorial of 4 is 24

Factorial of a Number  using Recursion in Python

# Python program to find  factorial of given number 
import math  
def fact(GivenNumber):  
    return(math.factorial(GivenNumber))  
  
num = int(input("Enter the number:"))  
Factorial = fact(num)  
print("Factorial of", num, "is",Factorial)

Output

Enter the number:7
Factorial of 7 is 5040

Exit mobile version