import math
print(math.factorial(4)) # 24
factorial of 4 is 24.
import math
print(math.factorial(12)) # 479001600
Factorial of a number without using the factorial() function
n=12
factorial=1
for i in range (1,n+1):
factorial=factorial * i
print(factorial)
Output is here
479001600
You can also calculate factorial using recursion:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
120
Factorial of 0 is defined as 1:
print(math.factorial(0))
1
For negative inputs, raise an error. More on try-except
try:
print(math.factorial(-3))
except ValueError as e:
print(e)
factorial() not defined for negative values
Factorial of a Number
import math
x=input("Enter the number : ")
if x.isdigit():
print(math.factorial(int(x)))
else:
print(" Enter correct number ")