Factorial of Each Number 1-N

Print factorial of each number from 1 to n.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 8 of 12
factorial-of-each-number-1-n.py
1# Take n
2n = int(input("Enter n: "))
3
4# Print factorials
5for i in range(1, n + 1):
6 fact = 1
7 for j in range(1, i + 1):
8 fact *= j
9 print(f"{i}! = {fact}")

Output

Enter n: 5
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120

What's going on

Calculate factorial for each number.

Key Concepts:

Outer loop: numbers 1 to n
Inner loop: calculate factorial
Print each factorial