Factorial with a While Loop in Python

To use a while loop to find the factorial of a number in Python:

  1. Ask a number input.
  2. Initialize the result to 1.
  3. Start a loop where you multiply the result by the target number.
  4. Reduce one from the target number in each iteration.
  5. End the loop once the target number reaches 1.

Here is how it looks in code:

def factorial(n):
    num = 1
    while n >= 1:
        num = num * n
        n = n - 1
    return num

Let’s test that it works:

f = factorial(5)
print(f)

Output:

120

There you have it!

To learn more about factorials and some alternative ways to calculate the factorial, please read along.

What Is the Factorial of a Number in Mathematics

In mathematics, the factorial is the product of all positive whole numbers less than or equal to a target number.

The factorial is denoted with an exclamation mark.

For example, let’s calculate the factorial of 5:

5! = 5 x 4 x 3 x 2 x 1 = 120

In layman’s terms, the factorial answers the question “In how many ways can you form a queue of n persons?”.

Recursion and Factorial in Python

In programming, recursion refers to a function that calls itself.

Usually, calculating the factorial acts as an introduction to recursion in programming courses.

This is because the factorial can be expressed recursively as:

n! = n x (n – 1)!

In this expression, there is a factorial operator (!) on both sides.

Essentially, this expression says “Factorial of n equals to n times the factorial of n – 1”.

Let’s use this as a basis to formulate a recursive factorial function in Python:

def factorial(n): 
    if (n==1 or n==0):
        return 1
    else:
        # n! = n x (n - 1)!
        return n * factorial(n - 1)

In this function:

  • If the target number is 0 or 1, we are going to return 1, because the factorial of 0 or 1 is 1.
  • If the target number is not 0 or 1, we are going to multiply it by the factorial of 1 less than the target.

Now you can test this code to make sure it works:

print(factorial(5))

Output:

120

Alternatives to Calculating the Factorial in Python

You now know what is the factorial of a number and how it is commonly calculated using recursion.

But there are some alternatives, that is:

  1. A built-in function
  2. While loop
  3. For loop

Let’s go through each of these approaches next.

1. Built-In Factorial Function in Python

Unless you are practicing your Python skills, you should use the built-in factorial function from the math module.

For example:

from math import factorial

print(factorial(5))

Output:

120

2. While Loop Factorial in Python

Any recursive function can be written as an iterative one.

In other words, you can use a regular loop instead of recursion.

Although, the feasibility to convert a recursive function into an iterative function varies based on the complexity of the function.

When talking about recursive factorial, it is trivial to convert it into an iterative one using a while loop.

All you need to do is:

  1. Create a loop where you start a result from 1
  2. Subtract 1 from the target number
  3. Multiply the result by it.
  4. Do this until the target reaches 1 and you have successfully computed the factorial.

Here is how it looks in code:

def factorial(n):
    num = 1
    while n >= 1:
        num = num * n
        n = n - 1
    return num

3. For Loop Factorial in Python

You can also convert a recursive factorial function to one that uses a for loop in Python.

To do this:

  1. Specify a target number.
  2. Set the result at 1.
  3. Start a for loop from 1 to the target number + 1.
  4. Multiply the result by each number in this range.
  5. Return the result.

Here is how it looks in code:

def factorial(n): 
    num = 1
    
    for i in range(1, n + 1):
        num = num * i
        
    return num

Example run:

print(factorial(5))

Output:

120

Conclusion

Today you learned how to calculate the factorial of a number using a while loop in Python.

To recap, the factorial of a number is a whole number multiplied by all positive whole numbers less than it.

Commonly, you see factorial being calculated with a recursion. But it is just as fine to calculate the factorial using an iterative approach, that is, a while or a for loop.

If you do not need to implement the factorial yourself, use the built-in math.factorial function in Python.

Thanks for reading.

Happy coding!

Further Reading

50 Python Interview Questions

Leave a Comment

Your email address will not be published. Required fields are marked *