Python Practice › Loop Programs › Program 30
Factorial with a loop
Write a Python program to calculate the factorial of a number using a loop.
Input
7
Output
7! = 5040
Approach
- Multiply 1 × 2 × … × n.
Solution
n = 7fact = 1for i in range(1, n + 1): fact *= iprint(f"{n}! = {fact}")Output
7! = 5040
Learn the concept
New to this topic? Read the lesson, then come back.