0
0
PythonProgramBeginner · 2 min read

Python Program to Print Floyd Triangle

You can print Floyd's triangle in Python using nested loops like for i in range(1, n+1): for j in range(i): print(num, end=' ') num += 1 print() where n is the number of rows.
📋

Examples

Input3
Output1 2 3 4 5 6
Input5
Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Input1
Output1
🧠

How to Think About It

To print Floyd's triangle, start with number 1 and print numbers in increasing order row by row. Each row has one more number than the previous row. Use two loops: the outer loop for rows and the inner loop for numbers in each row, increasing the number after each print.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
Initialize a variable num to 1 to start counting.
3
For each row from 1 to n:
4
Print numbers starting from num up to the count of the current row.
5
Increase num after printing each number.
6
Move to the next line after each row.
💻

Code

python
n = int(input('Enter number of rows: '))
num = 1
for i in range(1, n + 1):
    for j in range(i):
        print(num, end=' ')
        num += 1
    print()
Output
Enter number of rows: 3 1 2 3 4 5 6
🔍

Dry Run

Let's trace the program for input n=3 to see how Floyd's triangle is printed.

1

Initialize

n=3, num=1

2

First row (i=1)

Print num=1, then num=2; move to next line

3

Second row (i=2)

Print num=2, num=3, then num=4; move to next line

4

Third row (i=3)

Print num=4, num=5, num=6, then num=7; move to next line

RowNumbers PrintedNext num value
112
22 34
34 5 67
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, controlling how many rows to print.

Step 2: Inner loop prints numbers in each row

The inner for loop runs as many times as the current row number, printing numbers sequentially.

Step 3: Increment number after each print

After printing each number, num is increased by 1 to keep the sequence continuous.

Step 4: Print new line after each row

A print() statement without arguments moves the cursor to the next line after each row.

🔄

Alternative Approaches

Using while loops
python
n = int(input('Enter number of rows: '))
num = 1
i = 1
while i <= n:
    j = 0
    while j < i:
        print(num, end=' ')
        num += 1
        j += 1
    print()
    i += 1
This uses while loops instead of for loops; logic is similar but syntax differs.
Using a single loop with arithmetic
python
n = int(input('Enter number of rows: '))
num = 1
for i in range(1, n + 1):
    line = ' '.join(str(x) for x in range(num, num + i))
    print(line)
    num += i
This builds each row as a string using range and join, then prints it; cleaner but less stepwise.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the inner loop runs i times for each row i, totaling about n*(n+1)/2 iterations, which is O(n^2).

Space Complexity

Only a few variables are used to track numbers and loops, so space complexity is O(1), constant space.

Which Approach is Fastest?

All approaches have similar time complexity O(n^2). Using string join may be slightly slower due to string operations but improves readability.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and clear logic
Nested while loopsO(n^2)O(1)Alternative syntax preference
Single loop with string joinO(n^2)O(1)Cleaner output formatting
💡
Use nested loops where the outer loop controls rows and the inner loop prints numbers in each row.
⚠️
Beginners often forget to increase the number after printing, causing repeated numbers in the triangle.