# factorial()

factorial is the product of all the positive integers less than or equal to input number.
!n is also denoted as factorial of a number .
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

## Recursive Factorial Method

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

## Handling Edge Cases

Factorial of 0 is defined as 1:

print(math.factorial(0))
1

## Error Handling

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

## Practice Questions

Find the factorial of an input number. check the number before using factorial() method.
import math
x=input("Enter the number : ")
if x.isdigit():
print(math.factorial(int(x)))
else:
print(" Enter correct number ")

Subscribe to our YouTube Channel here

## Subscribe

* indicates required
Subscribe to plus2net

plus2net.com