How to Find Factorial of a Number in Python?

Factorial is denoted as exclamation marks “!” normally in mathematics. Factorial is defined as the product of all positive integer numbers smaller than or equal to the input number i.e., the factorial of the number 3 looks like this: “3x2x1=6”.

The factorial of a negative integer can not be defined, and the factorial of “0” is always “1”. To find the factorial of a number, different inbuilt functions, and user-defined approaches are used in Python.

This write-up will provide a detailed analysis of how to get the Python factorial of a number. The following content is demonstrated with numerous examples.

So, let’s begin!

How to Find the Factorial of a Number in Python?

To find the factorial of any input positive integer number, various methods are used in Python. One of the easiest and simplest methods to get the factorial of a number is using the “factorial()” function of the standard math module. Alternatively, the “Recursion” approach or a program with the “if-else” statement can also be used to get the factorial of the number. An open-source “numpy” library also provides a “numpy.prod()” function to find the factorial.

As a first step, let’s start with the following method:

Method 1: Using factorial() Function

In the example given below, the “factorial()” function is used to find the factorial of an integer number in Python.

Code:

import math 
 
number = 5
#using factorial()
factorial_num = math.factorial(number) 
print("Factorial of", number, "is", factorial_num)

In the above code:

  • The math module function named “math.factorial()” finds the factorial of a given number by taking its value as an argument.
  • The function only accepts a positive integer value as an argument.

Output:

The above output shows the factorial of the given number.

Method 2: Using Recursion Function

In the following example, the recursion function calls itself to calculate the factorial of the input number.

Code:

def factorial(val): 
    return 1 if (val==1 or val==0) else val * factorial(val - 1); 
 
Number = 6 
print("Factorial of",Number,"is", factorial(Number))

In the above code:

  • The function named “factorial” is defined in the program using the keyword “def”.
  • The return statement retrieves the value “1” if the input value is equal to “1” or “0”. If the value is not equal to “0” or “1”, then the else block statement executes.
  • The else block has the following statement “num * factorial(num – 1)”, which is used to find the factorial of a number.
  • The “print()” function prints the calculated factorial by accepting the factorial function as a parameter value.

Output:

The above output shows the factorial of the number “6”.

Method 3: Using if-else Statement

In the example given below, multiple conditions are used to calculate the factorial of the input number.

Code:

Number = int(input("Enter a number: "))   
factorial_value = 1   

if Number < 0:   
   print(" Factorial Does Not Exist")   

elif Number == 0:   
   print("Factorial of 0 is 1")   

else:   
   for num in range(1,Number + 1):   
       factorial_value = factorial_value*num   
   print("Factorial of Input Number is: ",factorial_value)   



In the above code:

  • A number will be taken from the user using “input()” function and the input value will be converted into integer using “int()” function.
  • The variable “factorial_value” is initialized with the initial value “1”.
  • The “if” and multiple “elif” statements are initialized with conditions. If the given number is less than “0”, the first “if” block executes its statement. Similarly, if the input number is equal to zero then the first “elif” block executes its statement.
  • The “else” block executes its statement when the input number is neither equal to zero nor one. The else block statement finds the factorial of the number and returns it to the variable named “factorial_value”.

Output:

The above output shows the factorial value of the user input number.

Method 4: Using numpy.prod() Function

In the example given below the “numpy.prod()” function of the “Numpy” library is used to find the factorial of an input number.

Code:

import numpy

Number = int(input("Enter a number: "))   
factorial = numpy.prod([i for i in range(1,Number+1)])
print('Factorial of Input Number is: ',factorial)

In the above code:

  • The numpy library is imported in the program, and the integer value is taken from the user using the “input()” function.
  • The “numpy.prod()” function finds the factorial of an input number.

Output:

The above output shows the factorial value of the user input number.

That’s it from this guide!

Conclusion

To find the factorial of a number, the inbuilt “factorial()” function, “Recursion” function, “if-else” statement, and “numpy.prod()” function is used in Python. The inbuilt “factorial()” function takes the number as a parameter and retrieves the factorial. The “Recursion” function can be defined and used to find the factorial of any input integer number by calling itself. The “if-else” statement and multiple conditions can be used to get the factorial of a number. Also, you can find the factorial of an integer number using the “numpy.prod()” function in Python. This article explained how to get the factorial of a number using multiple methods in Python.