AppDividend
Latest Code Tutorials

Python factorial() Example | How To Find Factorial In Python

0

Python factorial() is an inbuilt math library method that is used to find factorial of any integer type number. We can find factorial of any number using the naive method, but it is easier to find factorial using the factorial() method. The factorial() method is under python math library, so if we want to use this method, we first will have to import the math library.

Python factorial()

Python offers a direct function that can compute the factorial of a number without writing the whole code for computing factorial.

Syntax

math.factorial(num)

The factorial() method takes one integer as an argument. If we provide float or any negative number, it will throw an error.

Return Value

The factorial() method returns the factorial of the desired number.

Program to find factorial using the naive method

See the following code.

# Taking input from user
num = int(input("Enter the number to find factorial: "))
# Declaring one temporary variable to store the answer
fact = 1

# Finding factorial of the given number
for i in range(1, num+1):
    fact = fact*i

print("Factorial of the given number ", num, " is: ", fact)

Output

Enter the number to find factorial: 5
Factorial of the given number  5  is:  120

In this example, we have first taken input from the user then we have declared one temporary variable fact to store the factorial of the given number.

As we know, the factorial of a given number N is N*N-1*N-2*…*1, so we have written this in a reverse manner in a for loop.

At last, we got the answer.

Program to find factorial using factorial() method

See the following code.

# app.py

# importing math library
import math
# Taking input from user
num = int(input("Enter the number to find factorial: "))

# Finding factorial of the given number
fact = math.factorial(num)

print("Factorial of the given number ", num, " is: ", fact)

Output

Enter the number to find factorial: 5
Factorial of the given number  5  is:  120

In this example, we have first taken input from the user after importing the math library. Then we have called factorial() method to find the factorial of the given number, and at last, we got the answer.

Program to find factorial for Non Integral Value

See the following code.

# app.py

import math

print("The factorial of 11.21 is : ", end="")

# raises exception
print(math.factorial(11.21))

Output

python3 app.py
The factorial of 11.21 is : Traceback (most recent call last):
  File "app.py", line 6, in <module>
    print(math.factorial(11.21))
ValueError: factorial() only accepts integral values

We will face the ValueError because factorial() function accepts an only integral value.

Conclusion

If you want to find factorial in Python, then you can either use math.factorial() function or use naive method.

See also

Python math.fabs()

Python math copysign()

Python math functions

Python math.sqrt()

Python math.floor()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.