Python Continuous Number Triangle Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A continuous number triangle (Floyd’s triangle) prints a growing row each time — with one shared counter k that never resets between rows.

Remember
Rule: k = 1; for i = 1..rows
      print k, then k += 1, for i times

1
2 3
4 5 6
7 8 9 10     ← rows = 4

After Program 19’s fixed-width fill triangle, this pattern focuses on a running counter that carries across rows.

How to Solve It

Set k = 1 once; on row i, print and increment k exactly i times.

MethodIdeaBest for
Running counterk = 1 before loops; print(k); k += 1 insideLearning, interviews
input() rowsSame loops; read rows at runtimeInteractive practice
Custom startSame shape; initialize k to any start valueSequence variants

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from 1 to i:
        print k (same line, spaced)
        k += 1
    print newline

Cheat sheet

GoalPattern
Set rows / counterrows = 4; k = 1
Outer loopfor i in range(1, rows + 1):
Inner loopfor j in range(1, i + 1):
Print + bumpprint(k, end=" "); k += 1
End rowprint()
Values per rowi
Total valuesn(n+1)/2 for n rows

Printing Numbers vs Starting a New Line

APIEffectUse for
print(k, end=" ")Stays on the same lineEach number
print()Ends the current lineAfter the inner loop

Print numbers with end=" ", then break once with print(). Putting print() inside the inner loop prints one number per line.

Live Preview

Change the row count and the continuous triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 4 rows · 10 values
1 
2 3 
4 5 6 
7 8 9 10 

Worked Walkthrough — rows = 3

Trace each outer i, how k changes, and the printed row.

ik before rowPrinted valuesPrinted row
1111
222, 32 3
344, 5, 64 5 6

Values per row = i. Counter k never resets. Total = 6 = 3×4/2.

Python Programs

Three complete programs: fixed rows = 4, input() rows, and custom start k = 10. Use View Output for sample results.

Example 1 — Fixed rows = 4

Hard-coded height — declare k = 1 once, then print and bump inside the inner loop.

Python
rows = 4
k = 1

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

1. Counter. k starts at 1 before any loop — it must not be reset inside the outer loop.

2. Growing row. Row i runs the inner loop i times.

3. Print then bump. print(k, end=" "); k += 1 each time; then print() ends the row.

Example 2 — input() Rows

Read rows at runtime; the continuous counter still starts at 1.

Python
rows = int(input("Enter the number of rows: "))
k = 1

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

1. Read rows. Convert the prompt answer with int().

2. Same counter. k = 1 still sits before the loops — only the height changes.

3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:

Safer input
raw = input("Enter the number of rows: ").strip()
try:
    rows = int(raw)
except ValueError:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if rows < 1:
    print("Enter a positive whole number.")
    raise SystemExit(1)

Example 3 — Custom Start k = 10

Same growing triangle, but the continuous sequence begins at 10.

Python
rows = 4
k = 10

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

1. Only k changes. Loops match Example 1 — just initialize k = 10.

2. Same total count. Still 10 values for rows = 4; they run from 10 through 19.

3. Try other starts. Set k = 100 or any integer once you understand the default.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset k

Broken sequence

If you set k = 1 inside the outer loop, every row restarts at 1 instead of continuing.

forgot k += 1

Repeated value

Without k += 1, the same number prints on every cell.

print() inside

Vertical numbers

If print() is inside the inner loop, each number lands on its own line.

print j

Wrong sequence

Printing j instead of k restarts each row at 1 — that is a different triangle.

rows = 1

Single value

Output is just 1 — one outer iteration, one print.

input()

Catch ValueError

Letters crash bare int(input()) — wrap in try/except and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Values per pattern = 1 + 2 + … + n = n(n+1)/2 → O(n²).

Key Takeaways

  • Rule: set k = 1 once; on row i, print and bump k for i times.
  • Shared counter: never reset k inside the outer loop.
  • end= vs print(): numbers stay on the line; print() advances after each row.
  • Complexity: total values = n(n+1)/2 → O(n²).

One line: grow the row length while a shared k keeps counting upward.

Frequently Asked Questions

Because k is assigned once (k = 1) before the loops and incremented with k += 1 every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so k becomes 2. Row 2 prints k twice: 2, then 3 after k += 1.
k resets each row and the sequence restarts at 1 — you get a growing 1, 1 2, 1 2 3 triangle, not a continuous counter.
print(k, end=" ") stays on the same line with a trailing space. print() ends the current line. Numbers use end=" "; the row break uses print() after the inner loop.
Total prints are 1+2+…+n = n(n+1)/2.
Yes. Set k = 10 (or any value) before the loops — see Example 3.
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use try/except ValueError around int(input()) so bad input does not crash the script.
Only one row prints — a single 1.

Did you know?

A single counter k starts at 1 and increments with k += 1 on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.

Next: Jump Number Triangle

Continue with the next pattern in the Python number-pattern series.

Program 21 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful