Print Floyd's Triangle in Python

Print Floyd's triangle (consecutive numbers in a right-angled triangle).

BeginnerLoop ProgramsExample 24 of 25
print-floyd-s-triangle.py
1# Program to print Floyd's triangle
2
3rows = int(input("Enter number of rows: "))
4
5num = 1
6for i in range(1, rows + 1):
7 for j in range(i):
8 print(num, end=" ")
9 num += 1
10 print()

Output

Enter number of rows: 4
1 
2 3 
4 5 6 
7 8 9 10 

What's going on

We keep a running counter and print increasing numbers row by row.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.