Program Overview
This Python program uses nested loops to generate a number pattern.
Each row prints the numbers from 1 up to the row number.
This structure is a good exercise for learning nested loops.
Python Code:
# Read the number of rows from the user
n = int(input("Enter the number of rows: "))
# Build the pattern with nested loops
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()Sample Output:
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Step-by-Step Explanation:
- The outer loop controls the number of rows
- The inner loop prints the numbers from 1 up to the current row
- end=" " keeps the numbers on the same line
- print() moves to a new line after each row