Python program to find factorial of a number

Description: Write a Python program to find the factorial of a number using a loop.

Rules and hints:

  • Factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
  • Start with 1 and multiply by each number.

Python program for factorial using a loop

num = 5
factorial = 1

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

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

Output:

Tutorials Class - Output Window

Factorial of 5 is 120

How it works

  • factorial starts with 1.
  • The loop multiplies it with 1, 2, 3, 4 and 5 one by one: 1, 2, 6, 24, 120.
  • For 0, the loop does not run, and the answer stays 1, which is correct.

You can also solve it with a function that calls itself. See Python Functions (recursion).


Learn more about the similar topics:
Tutorials
Python Conditional Statements
Python While Loop
Python for loop
Python Jump Statements
Exercises & Assignments
Python program to check even or odd number
Python program to find the largest of three numbers
Python program to check leap year
Python program to print multiplication table
Python program to find factorial of a number
Python program to print Fibonacci series
Python program to check prime number
Interview Questions & Answers
No Content Found.