This Python program uses nested loops to print a triangle of numbers.
Each row begins with a circle symbol ◯ followed by an increasing sequence of numbers starting from 1.
The number of elements in each row increases by one until the final row.
# Read number of rows from user
n = int(input("Enter number of rows: "))
# Generate pattern using nested loops
for i in range(1, n + 1):
print("◯", end=" ")
for j in range(1, i + 1):
print(j, end=" ")
print()
Enter number of rows: 8
◯ 1
◯ 1 2
◯ 1 2 3
◯ 1 2 3 4
◯ 1 2 3 4 5
◯ 1 2 3 4 5 6
◯ 1 2 3 4 5 6 7
◯ 1 2 3 4 5 6 7 8
- The outer loop controls the number of rows
- Each row starts with the symbol ◯
- The inner loop prints numbers from 1 up to the current row index
- end=" " keeps output on the same line
- print()