Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Factorial?
3.
Methods to Find the Factorial of a Number in Python
3.1.
1. Using Factorial Function in Python
3.2.
Python
3.3.
2. Using For Loop in Python Factorial program
3.4.
Python
3.5.
3. Using while Loop in Python Factorial program
3.6.
Python
3.7.
4. Using Recursion in Python Factorial program
3.8.
Python
3.9.
5. Using Ternary Operator in Python Factorial program
3.10.
Python
3.11.
6. Using the Math Module in Python Factorial program
3.12.
Python
4.
Applications of Factorial Function in Python
5.
Frequently Asked Questions
5.1.
What is the factorial of 6 in Python?
5.2.
What is the math factorial command in Python? 
5.3.
How to calculate factorial of a number using for loop in Python? 
6.
Conclusion 
Last Updated: May 2, 2024
Easy

Python Program to Find the Factorial of a Number

Introduction

In mathematics and computer science, factorials serve as fundamental constructs, encapsulating the essence of combinatorial calculations and recursive algorithms. From simple mathematical expressions to complex computational problems, understanding how to compute factorials is essential for aspiring programmers.

In this article, we will discuss all the different ways to run a factorial program in Python using for loop, while loop, Recursion, ternary operator, and the Python built-in factorial function.

factorial program in python

What is a Factorial?

The factorial of a number is a numeric calculation that includes the multiplication or the product of the number by all positive numbers less than the number itself. Sometimes it is shown with the symbol “!”.

Factorial can be calculated using the following recursive formula.

n! = n * (n-1)!

n1 = 1 if n = 0 or n = 1

For example, a factorial of 5 can be written as 5! which is equal to 5x4x3x2x1 = 120

The Factorial of a number is often used in probability theory to calculate the number of combinations or sets of objects.

In Indian mathematics, the Jain texts have described the concept of factorial as “Gyarah Mala” and have stated methods for its calculation using both iterative and recursive approaches. 

Methods to Find the Factorial of a Number in Python

There are several methods to calculate the factorial of a number in Python; below are some of them.

1. Using Factorial Function in Python

The factorial function in Python is a built-in mathematical function used to compute the factorial of a non-negative integer. It calculates the product of all positive integers up to and including the specified number. The factorial function is commonly denoted by the exclamation mark (!).

  • Python

Python

# Import the factorial function from the math module
from math import factorial

# Input a number from the user
num = int(input("Enter a number: "))

# Calculate the factorial using the factorial function
result = factorial(num)

# Output the result
print("Factorial of", num, "is:", result)

Output:

Enter a number: 5
Factorial of 5 is: 120

 

Explanation:

  • We import the factorial function from the math module using from math import factorial.
  • The user inputs a number, which is stored in the variable num.
  • We calculate the factorial of the input number using the factorial() function and store the result in the variable result.
  • Finally, we output the result, displaying the factorial of the input number.

2. Using For Loop in Python Factorial program

Factorial program in Python using the for loop.

Code:

  • Python

Python

#Enter the number
n = int(input("Enter the number : "))

fac = 1
if n == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1, n + 1):
       fac = fac * i
   print("The factorial of the above number is",fac)

 

Output:

Enter the number : 5
The Factorial of the above number is 120

 

Time Complexity = O(n)

Space Complexity = O(1)

Explanation: Factorial of 5 = 1x2x3x4x5 = 120
 

3. Using while Loop in Python Factorial program

Factorial program in Python using the For loop.

Code:

  • Python

Python

#Enter the number
n = int(input("Enter the number : "))

fac = 1
if n == 0:
   print("The factorial of 0 is 1")
else:
 while(n > 0):
       fac = fac * n
       n = n - 1
 print("The factorial of the above number is",fac)

 

Output:

Enter the number : 6
The Factorial of the above number is 720

 

Time Complexity = O(n)

Space Complexity = O(1)

Explanation: Factorial of 5 = 1x2x3x4x5x6 = 720

4. Using Recursion in Python Factorial program

We can also write the factorial program in Python using the recursion method.

Code:

  • Python

Python

def fac_recursion(x):
  if (x==1):
      return x
  else:
      #the fac_recursion() function is recursively called to calculate the factorial of x
      return x * fac_recursion(x-1)

#Enter the number
n = int(input("Enter the number : "))
print("The factorial of the above number is", fac_recursion(n))

 

Output:

Enter the number : 5
The Factorial of the above number is 120

 

Time Complexity = O(n)

Space Complexity = O(n)

Explanation: Factorial of 5 = 1x2x3x4x5 = 120

5. Using Ternary Operator in Python Factorial program

Factorial program in Python using the Python ternary operator.

Code:

  • Python

Python

def fac(x):
   #Using ternary operator to calculate the factorial
   return 1 if (x==1 or x==0) else x * fac(x-1);

#Take the number as an input from the user
num = int(input("Enter the number : "))

print("The factorial of the above number is", fac(num))

 

Output:

Enter the number : 10
The Factorial of the above number is 3628800

 

Time Complexity = O(n)

Space Complexity = O(n)

Explanation: Factorial of 10 = 1x2x3x4x5x6x7x8x9x10 = 3628800

6. Using the Math Module in Python Factorial program

We can also write the factorial program in Python using the built-in factorial function available in the math module.

Code:

  • Python

Python

import math  
def fac(x):
   #using the built-in factorial() function to calculate factorial
   return(math.factorial(x)) 

#Enter the number 
n = int(input("Enter the number : ")) 
f = fac(n) 
print("Factorial of the above number is", f)

  

Output:

Enter the number : 4
The Factorial of the above number is 24

 

Time Complexity = O(n)

Space Complexity = O(1)

Explanation: Factorial of 4 = 1x2x3x4 = 24

Applications of Factorial Function in Python

Applications of the factorial function in Python span various domains, including mathematics, computer science, and practical problem-solving scenarios. Here are some common applications:

  • Combinatorics: Factorials are fundamental in combinatorial mathematics, where they represent the number of ways to arrange or select elements. They are used in permutations, combinations, and binomial coefficients calculations.
  • Probability: Factorials are employed in calculating probabilities, especially in problems involving permutations and combinations. They help determine the likelihood of certain events occurring in experiments or random processes.
  • Algorithms: Factorials play a crucial role in the design and analysis of algorithms, particularly in recursive algorithms and dynamic programming solutions. They are utilized in problems such as calculating Fibonacci numbers, finding shortest paths in graphs, and solving optimization tasks.
  • Series Expansion: Factorials are integral in expressing mathematical functions as series expansions, such as Taylor series. They aid in approximating complex functions and solving differential equations numerically.
  • Simulation and Modeling: Factorials are utilized in simulation and modeling tasks to represent factorial designs, factorial experiments, and factorial analysis. They help investigate interactions between multiple factors and variables in experimental studies.
  • Cryptographic Functions: Factorials are utilized in cryptographic algorithms and protocols for generating large prime numbers, calculating permutations, and verifying the correctness of cryptographic operations.

Frequently Asked Questions

What is the factorial of 6 in Python?

In Python, the factorial of 6 is 720. You can calculate it using Python code or by using the math.factorial function.

What is the math factorial command in Python? 

Python's math.factorial function is a built-in method for calculating factorials. It takes an input number and returns the factorial value.

How to calculate factorial of a number using for loop in Python? 

compute the factorial of a number using a for loop in Python, you can create a function that iterates from 1 to the given number, multiplying each iteration's value to calculate the factorial.

Conclusion 

In this article, we discussed the Python Program to Find the Factorial of a Number. This problem opens doors to a multitude of applications across mathematics, computer science, and practical problem-solving domains. Through the use of Python's built-in factorial() function or custom implementations, developers can efficiently calculate factorials of numbers and leverage this fundamental concept in combinatorial calculations, probability analysis, algorithm design, and more.

We hope this blog has helped you get the different approaches to finding the factorial of a number in Python at your fingertips. Keep learning! We recommend you read some of our other articles on Python: 

  1. Palindrome number in Python
  2. Functions in Python
  3. Data Structures in Python
Live masterclass