In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number.
Syntax: math.factorial(x)
Parameter:
x: This is a numeric expression.
Returns: factorial of desired number.
Time Complexity: O(n) where n is the input number.
Auxiliary space: O(1)
Code #1:
Python3
import math
x = 5
print ("The factorial of 5 is : ", end ="")
print (math.factorial(x))
|
Output:
The factorial of 5 is : 120
Code #2:
Python3
import math
x = 5
y = 15
z = 8
print ("The factorial of 5 is : ", math.factorial(x))
print ("The factorial of 15 is : ", math.factorial(y))
print ("The factorial of 8 is : ", math.factorial(z))
|
Output:
The factorial of 5 is : 120
The factorial of 15 is : 1307674368000
The factorial of 8 is : 40320
Code #3: Throws ValueError if x is not integer
Python3
import math
print ("math.factorial(13.7) : ", math.factorial(13.7))
|
Output:
ValueError: factorial() only accepts integral values
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...