Factorial Function in Python

Factorial function in python | In this program, we will discuss how to find the factorial of a number using a function in python. Also, discuss how to find the factorial of a number using math.factorial() function.

Factorial of a positive integer number specifies a product of all integers from 1 to that number. It is defined by the symbol exclamation mark (!).

General case for finding factorial

factorial(n) = n * factorial(n-1)

The base case for finding factorial

factorial(0) = 1
Or,
factorial(1) = 1

Factorial (n) = 1 * 2 * … * (n-1) * n
Or, n * (n-1) * … * 2 * 1

The factorial of a negative number and float number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.

The factorial of 6 is denoted as 6! = 1*2*3*4*5*6 or 6*5*4*3* 2*1

We can write 6! as,
6! = 6 * 5!
Similarly, we can write
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1

Factorial Program in Python using Function

In this program, We can also take the help of a user-defined function. A function is a block of code that performs a specific task.

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

def factorial(num):  #user-defined function
    # find factorial of a number
    fact = 1
    for i in range(1,num + 1):
       fact = fact*i
    return fact

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

# check number is positive, negative, or zero
if num < 0:
   print('Factorial does not exist for negative numbers')
elif num == 0:
   print('The factorial of 0 is 1')
else:
    # calling function
    print('The factorial of',num,'is',factorial(num))

Output for the different input values:-

Enter number: 4
The factorial of 4 is 24

Enter number: -4
Factorial does not exist for negative numbers

Enter number: 6
The factorial of 6 is 720

In this program, we will be the first defined function.

def factorial(num):  #user-defined function
    # find factorial of a number
    fact = 1
    for i in range(1,num + 1):
       fact = fact*i
    return fact

Inputs are scanned using the input() function and stored in variable num.

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

Check number is positive, negative, or zero. Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1. The number is positive then it passed to the factorial() function to compute the factorial of the number and the result will be displayed on the screen.

if num < 0:
   print('Factorial does not exist for negative numbers')
elif num == 0:
   print('The factorial of 0 is 1')
else:
    # calling function
    print('The factorial of',num,'is',factorial(num))

Program using Math Function

In the Math module of Python, the factorial() function is already available, which calculates the factorial of the given number and returns the result. See more:- Math.factorial() in Python.

In this program, we will find the factorial of a number using math.factorial() function. The factorial() function will be imported from the math file.

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

import math   #math module

def factorial(num):
    # find factorial of a number
    print('The factorial of',num,'is', math.factorial(num))

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

# calling function
factorial(num)

Output for the different input values:-

Enter number: 3
The factorial of 3 is 6

Enter number: 0
The factorial of 0 is 1

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

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

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