This post shows how to find the factorial of a number in Python using a simple loop. The factorial of a number n (written n!) is the product of all positive integers from 1 to n.

Find factorial of a number in Python:

Explanation:
We take a number as input, then multiply it with every integer below it down to 1, using a for loop and a running product variable initialized to 1.


Python Program to Find Factorial of a Number

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

if num < 0:
    print("Factorial doesn't exist for negative numbers")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1, num + 1):
        factorial *= i
    print(f"The factorial of {num} is {factorial}")

Output:

Enter a number: 5
The factorial of 5 is 120

You can test this and modify it yourself in our Code Runner — no installation needed.