Write a Python Program to Find the Factorial of a Number

Factorial is a mathematical operation that finds the product of all positive integers less than or equal to a given number.

It is denoted by the exclamation mark (!) after the number.

For example, the factorial of 5 is represented as 5! and is equal to 5 × 4 × 3 × 2 × 1, which is equal to 120.

In this tutorial, we will learn how to find the factorial of a number using Python programming language.


We can find the factorial of a number using a for loop in Python.

The algorithm to find the factorial of a number is as follows:

  1. Initialize a variable to store the factorial value as 1
  2. Use a for loop to iterate from 1 to the given number
  3. Multiply the factorial value by the loop variable in each iteration
  4. Print the factorial value after the loop completes

Here’s the Python program to find the factorial of a number:

# Python program to find the factorial of a number

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

# initialize factorial value to 1
factorial = 1

# loop through 1 to num
for i in range(1, num + 1):
    factorial = factorial * i

print("Factorial of", num, "is", factorial)

In this program, we first take user input for the number whose factorial we want to find.

Then, we initialize the factorial value to 1.

We use a for loop to iterate from 1 to the given number and multiply the factorial value by the loop variable in each iteration.

Finally, we print the factorial value.

Let’s run the program and see the output for the factorial of 5:

Enter a number: 5
Factorial of 5 is 120

As we can see, the program correctly calculates the factorial of 5 as 120.


In conclusion, we have learned how to find the factorial of a number using Python programming language.

We used a simple algorithm and a for loop to calculate the factorial value.

The program can be easily modified to find the factorial of any given number.