AppDividend
Latest Code Tutorials

Python factorial: How to find Factorial of Number

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

Python factorial

To calculate the factorial of any integer in Python, use the in-built factorial() method. The factorial() method belongs to the math library of Python that’s why if we want to use the factorial() method then will have to import the math library first.

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.

Python Program to find factorial using the native approach

# 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.

Python Program to calculate factor 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 the factorial() method to find the factorial of the given number, and at last, we got the answer.

Python Program to find factorial for Non-integer 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 the 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.