Write a Program in Python to find the Factorial of a Number
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 |
# 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