Python Math Factorial Program – math.factorial()

Python Math Factorial Function | In Python, math.factorial() method is a library method of the math module, it is used to find the factorial of a number. It accepts a positive integer number and returns the factorial of the number.

This method accepts only a positive integer value, not a negative integer value. Also, this method accepts integer value not working with float value. If the value is either a negative or float, it returns “ValueError”. If the value of the number is 0 then its factorial will be 1.

Syntax:-

math.factorial(num)

Parameter:-

num: Any positive integer. If the number is negative, or not an integer,
it returns a ValueError. If the value is not a number, it returns a TypeError

Returns:- factorial of desired number.

Factorial Program in Python using math.factorial()

This is the simplest and easiest method to find the factorial of a number. Because we will give a number and math.factorial() function will return the factorial value of the given number.

# Python program to find the factorial of a number using math function

import math   #math module

# take input
num = 5

# find factorial of a number and display result
print('The factorial of',num,'is', math.factorial(num))

Output:-

The factorial of 5 is 120

In this program, first import math module.

import math   #math module

We have hardcoded the values of the number in the source code, 5 numeric values are assigned to them.

num = 5

Find factorial of a number using math.factorial() function and result will be displayed on the screen.

print('The factorial of',num,'is', math.factorial(num))

If we will try to find the factorial of a negative number then the python program will return “ValueError”. Because, math.factorial() not defined for negative values.

import math
num = -2
print(math.factorial(num))

Output:-

Traceback (most recent call last):
File “main.py”, line 3, in
print(math.factorial(num))
ValueError: factorial() not defined for negative values

If we will try to find the factorial of a float value then python program will return “ValueError”. Because, math.factorial() only accepts integral values.

import math
num = 5.2
print(math.factorial(num))

Output:-

Traceback (most recent call last):
File “main.py”, line 3, in
print(math.factorial(num))
ValueError: factorial() only accepts integral values

Program using Math Function (by taking input From user)

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to find the factorial of a number using math function

import math   #math module

# take input
num = int(input("Enter number: "))

# find factorial of a number and display result
print('The factorial of',num,'is', math.factorial(num))

Output for the different input values:-

Enter number: 0
The factorial of 0 is 1

Enter number: 3
The factorial of 3 is 6

Enter number: -5
Traceback (most recent call last):
File “main.py”, line 9, in
print(‘The factorial of’,num,’is’, math.factorial(num))
ValueError: factorial() not defined for negative values

Enter number: 2.6
Traceback (most recent call last):
File “main.py”, line 6, in
num = int(input(“Enter number: “))
ValueError: invalid literal for int() with base 10: ‘2.6’

In the above program, find the factorial of a number in python using for loop and while loop but many different ways to find factorial of a number in python.

Similar Program to find factorial in Python:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply