What is a Factorial?

The product of all positive integers is defined as the factorial of a non-negative integer "n". from 1 to "n."

For example:

0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

Factorials are used in mathematics and programming for various calculations and counting tasks.

Here is an example of the Python program for the factorial of a number: 

def factorial(n):

    if n == 0:

        return 1

    else:

        return n * factorial(n - 1)

# Example usage:

number = 5

result = factorial(number)

print(f"The factorial of {number} is {result}")

Factorial Program in Python Using Recursion

A factorial program in Python using recursion calculates the factorial of a number by breaking the problem into smaller subproblems. To determine the factorial, it makes use of a function that calls itself. Recursion is a method by which a function resolves larger instances of the same issue to solve the original problem.

Example:

def factorial(n):

    return 1 if n == 0 else n * factorial(n - 1)

# Example usage:

number = 5

result = factorial(number)

print(f"The factorial of {number} is {result}")

In this program:

  1. The parameter for the `factorial` function is an integer, {n}.
  2. Since 0! is specified as 1, it gives a straightforward example where it returns 1 when {n} is 0.
  3. It multiplies {n} by the factorial of {n - 1} to determine the factorial for any other value of {n}. 
  4. The program computes the factorial of the number 5 and prints the result to show how to use the `factorial} function.

Factorial Program in Python Using Built-in Function

Utilising the factorial function from the math module, a Python program may compute a number's factorial using an integrated function, making the process of determining a number's factorial easier.

Example:

import math

# Example usage:

number = 5

result = math.factorial(number)

print(f"The factorial of {number} is {result}")

In this program:

  1. The math module provides A built-in factorial function, which we load.
  2. The factorial of the given integer is immediately computed using math.factorial() function.
  3. The program calculates and prints the factorial of the number 5.

Factorial Program in Python Using for Loop

A Python program for calculating factorials using a `for` loop iterates through the numbers from 1 to the given input and accumulates their product.

Example:

def factorial(n):

    result = 1

    for i in range(1, n + 1):

        result *= i

    return result

# Example usage:

number = 5

result = factorial(number)

print(f"The factorial of {number} is {result}")

In this program:

  1. The input for the `factorial` function is an integer {n}.
  2. It sets the product's initialisation value, {result}, to 1.
  3. The `for` loop multiplies each number by the {result} as it iteratively goes through the integers from 1 to {n}.
  4. The program computes the factorial of 5 and prints the result to illustrate the use of the `factorial} function.

Factorial Program in Python Using While Loop

A Python program for calculating factorials using a `while` loop multiplies the numbers from 1 to the given input and accumulates their product iteratively.

Example:

def factorial(n):

    result = 1

    while n > 0:

        result *= n

        n -= 1

    return result

# Example usage:

number = 5

result = factorial(number)

print(f"The factorial of {number} is {result}")

In this program:

  1. The input for the `factorial` function is an integer {n}.
  2. It sets the product's initialisation value, {result}, to 1.
  3. The `while` loop multiplies {n} by the {result} repeatedly and decreases {n} until it equals 0.
  4. The program computes the factorial of 5 and prints the result to illustrate the use of the `factorial} function.

Factorial Program in Python Using Math Module – Factorial()

A simple method for computing factorials without the need for explicit loops or recursion is to use the factorial() function from the math module in a Python program to get the factorial of a given integer.

Example:

import math

# Example usage:

number = 5

result = math.factorial(number)

print(f"The factorial of {number} is {result}")

In this program:

  1. To calculate factorials, we load the math module with an integrated factorial function.
  2. The factorial of the given integer is immediately computed using math.factorial() function.
  3. Using the factorial() function from the math module, the program determines and outputs the factorial of the integer 5.

Conclusion

Python is versatile enough to accommodate various coding needs and preferences thanks to its several techniques for calculating a number's factorial. The following options are available to you:

  • Recursion: Recursive functions are elegant but may have limitations for large factorials.
  • For Loop: ‘For’ loops provide a transparent and efficient way to compute factorials for most practical cases.
  • While Loop: ‘while’ loops can be used for iterative factorial calculation and are especially useful in scenarios where loop termination conditions are more complex.
  • Using ‘math’ Module: The 'factorial()' function from the 'math' module offers a high degree of precision and ease while streamlining the procedure for simple factorial computations.

The particular use case, readability, efficiency, and the magnitude of the numbers involved all influence the technique selected. Overall, Python provides flexibility and simplicity for implementing factorial calculations.

If you are looking to enhance your software development skills further, we would highly recommend you to check Simplilearn’s Professional Certificate Program in Full Stack Web Development. This program, in collaboration with IIT Madras, You can acquire the necessary skills and become prepared for a job quickly with my help.

If you have any questions or queries, feel free to post them in the comments section below. Our team will get back to you at the earliest.

FAQs

1. How do you write a factorial in Python? 

 To calculate the factorial of a number in Python, you can use the built-in `math.factorial()` function, like this:

```python

import math

number = 5  # Replace 5 with the desired number

result = math.factorial(number)

```

Alternatively, you can use a `for` loop, a `while` loop, or a recursive function to calculate the factorial.

2. How do you calculate factorial in Python using a for loop?

You can calculate the factorial of a number in Python using a `for` loop like this:

```python

def factorial(n):

    result = 1

    for i in range(1, n + 1):

        result *= i

    return result

```

Replace `n` with the desired number, and the function will return the factorial.

3. What is recursion and factorial in Python?

  • Recursion in Python: A technique where a function calls itself to solve a problem.  
  • Factorial in Python: The product of all positive integers from 1 to a given number.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 29 May, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 18 Jun, 2024

6 Months$ 1,449

Get Free Certifications with free video courses

  • Python for Beginners

    Software Development

    Python for Beginners

    10 hours4.5274K learners
  • Introduction to Data Science

    Data Science & Business Analytics

    Introduction to Data Science

    7 hours4.665.5K learners
prevNext

Learn from Industry Experts with free Masterclasses

  • Prepare for Digital Transformation with Purdue University

    Business and Leadership

    Prepare for Digital Transformation with Purdue University

    11th Jan, Wednesday9:00 PM IST
  • Program Preview: Post Graduate Program in Digital Transformation

    Career Fast-track

    Program Preview: Post Graduate Program in Digital Transformation

    20th Jul, Wednesday9:00 PM IST
  • Expert Masterclass: Design Thinking for Digital Transformation

    Career Fast-track

    Expert Masterclass: Design Thinking for Digital Transformation

    31st Mar, Thursday9:00 PM IST
prevNext