Factorial of a Number in Python Using While Loop

Factorial is a method for calculating the total number of alternative arrangements for a given set of items. It is calculated by multiplying all positive whole numbers up to the designated number. 

For example, the factorial of 5, which is written as 5! means multiplying 5 x 4 x 3 x 2 x 1, which equals 120. In mathematics and probability, factorials are used to calculate permutations and combinations, and predicting a certain odds of an event.

In today’s lesson, we are going to learn how to get the factorial of a number in Python Using While Loop.

What is the While Loop in Python?

The while loop in Python is a controlled flow statement that allows you to repeatedly execute a part of code as long as a specified condition is true. Once a condition is false, the while loop exits. Let us look at the Python program to find the factorial of a number using While Loop.

Python Program to Print the Factorial of a Number Using While Loop

# accept input number from user
n = int(input("Enter any number: "))

# logic to calculate the factorial of a number
f = 1
while n >= 1:
    f *= n
    n -= 1

# print output
print("Factorial is", f)

The below is the logic as well as the Python program to find the factorial of a number using While Loop. In the example code, we are using 5 as our variable.

  • Initializing the ‘n’ variable
    • We will first initialize the code and input the value for the variable (which is going to be denoted by n in the code) equal to 5
    • Syntax : n = int (input(“Enter any number: “))
  • Assigning the ‘f’ variable
    • After we assign the desired value to the variable ‘n’, we will denote the factorial variable as 1. This is vital because we are going to multiply ‘n’ by the value of ‘factorial’.
  • Implementing the ‘while’ loop
    • We then enter the ‘while’ loop that continues as long as ‘n’ is greater than 1. We want to repeat the multiplication until we reach n=1
    • In the loop, we are going to multiply the current value of ‘f’ variable by ‘n’ variable that is f*n mathematically, which can be written as ‘*=’ operator in Python.
    • This gives us the value of the ‘f’ as the product of all the numbers prior to itself.
    • We then reduce the value of ‘n’ by 1 for each iteration of the sequence by using the ‘-= 1’ operator. 
    • Once the variable ‘n’ reaches 1, the loop terminates and gives out a result.
  • Obtaining the result
    • In the final step, we print the value of the factorial with the help of ‘print(“Factorial is”, f)’ command.


Enter any number: 5
Factorial is 120

The output obtained as mentioned in the screenshot above.

The code in the example gives out the result 120 which is indeed the factorial of number 5. 

Advantages of using while loops in Python:

  • Ease of Execution: While defining the while loop to obtain the factorial of a number, programmers need not know the exact number of iterative multiplications needed to output an accurate result.
  • Condition evaluation: The condition of a while loop can be any expression that evaluates to a Boolean value, which means that you can use variables, functions, or any other complex expression.
  • Incremental processing: While loops are often used for incremental processing, where the loop executes a sequence of steps that depend on the results of previous iterations. Which is vital to calculate the Factorial of a number.

Disadvantages of using while loops in Python:

  • Code complexity: While loops can make the code more complex and harder to read and maintain, especially if the loop body contains nested loops, conditional statements, or complex expressions. 
  • Difficult to Optimise: Due to its code complexity while loops are harder to debug or to optimize the code.
  • Performance overload: While loops can be slower than other loop constructs such as for loops, especially if the loop body is executing a large number of instructions or if the condition is evaluating a complex expression. 

In this tutorial, we learned about the logic and the execution of the logic to obtain the factorial of a number in Python using While Loop. We weighed its disadvantages to the for loops and its certain advantages. The while loop is a resource-heavy operation, however, it simplifies the recursive multiplication required to obtain the factorial of a number.

About The Author

Leave a Reply