Home » Python

math.factorial() method with example in Python

Python math.factorial() method: Here, we are going to learn about the math.factorial() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019

Python math.factorial() method

math.factorial() method is a library method of math module, it is used to find the factorial of a given number, it accepts a positive integer number and returns the factorial of the number.

Note:

  • The method accepts only integer (positive) value, if the value is either a negative or float – it returns "ValueError".
  • If the number is 0 – its factorial will be 1.

Syntax of math.factorial() method:

    math.factorial(n)

Parameter(s): n – a positive integer number.

Return value: int – it returns factorial of given number n.

Example:

    Input:
    a = 6

    # function call
    print(math.factorial(a))

    Output:
    720

Python code to demonstrate example of math.factorial() method

# Python code to demonstrate example of 
# math.factorial() method

# importing math module
import math

# numbers
a = 0
b = 1
c = 6
d = 13

# printing factorial
print("factorial of ", a, " is = ", math.factorial(a))
print("factorial of ", b, " is = ", math.factorial(b))
print("factorial of ", c, " is = ", math.factorial(c))
print("factorial of ", d, " is = ", math.factorial(d))

Output

factorial of  0  is =  1
factorial of  1  is =  1
factorial of  6  is =  720
factorial of  13  is =  6227020800

ValueError: factorial() not defined for negative values

If we try to find the factorial of a negative integer value – method will return this error.

# Python code to demonstrate example of 
# math.factorial() method

# importing math module
import math

# -ve integer 
a = -5

print(math.factorial(a))

Output

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    print(math.factorial(a))
ValueError: factorial() not defined for negative values

ValueError: factorial() only accepts integral values

If we try to find the factorial of a float value – method will return this error.

# Python code to demonstrate example of 
# math.factorial() method

# importing math module
import math

# -ve integer 
a = 5.1

print(math.factorial(a))

Output

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    print(math.factorial(a))
ValueError: factorial() only accepts integral values





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.