Python Number Triangle Pattern (Consecutive Offset)

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

What Is This Pattern?

An increasing number triangle from 1 prints i values on row i, each computed as i + j - 1 — so each row starts with its own row number.

Remember
Rule: for i from 1 to rows
        for j from 1 to i
          print (i + j - 1) and a space

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

Follows the triangle from 11 in Program 32; next is the variant from 0 in Program 34.

How to Solve It

Outer loop grows i. Inner loop prints i values with the formula i + j - 1, separated by spaces.

MethodIdeaBest for
Formula i + j - 1When j = 1, value equals i — row starts at its indexLearning, interviews
input() rowsSame loops; read rows at runtimeInteractive practice
Compact rows = 3Same structure; easier to trace by handPaper walkthrough

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (i + j - 1) and a space
    print newline

Cheat sheet

GoalPattern
Grow each rowfor i in range(1, rows + 1):
Print i valuesfor j in range(1, i + 1):
Start at 1print(i + j - 1, end=" ")
Row starts at iWhen j = 1, i + j - 1 = i
End of rowprint()

Printing Numbers vs Starting a New Line

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

Print each value with a trailing space, then break once with print(). Putting print() inside the inner loop prints one number per line.

Live Preview

Change the row count and the increasing triangle updates instantly.

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

Live result rows = 5 · 15 numbers
1 
2 3 
3 4 5 
4 5 6 7 
5 6 7 8 9 

Worked Walkthrough — rows = 5

Trace how each (i, j) pair maps to i + j - 1.

iValuesCountPrints
1111
22, 322 3
33, 4, 533 4 5
44..744 5 6 7
55..955 6 7 8 9

Total numbers = 1 + 2 + … + n = n(n+1)/2 → O(n²).

Python Programs

Three complete programs: fixed rows = 5, input() rows, and compact rows = 3. Use View Output for sample results.

Example 1 — Fixed rows = 5

Hard-coded height — print i + j - 1 with a trailing space on each cell.

Python
for i in range(1, 6):
    for j in range(1, i + 1):
        print(i + j - 1, end=" ")
    print()

How It Works

1. Outer grows the width. Row i prints exactly i numbers.

2. Formula fills the cells. i + j - 1 makes row i start at i and count upward.

3. Newline once. Call print() only after the inner loop finishes.

Example 2 — input() Rows

Read rows at runtime, then use the same formula core.

Python
rows = int(input("Enter rows: "))

if rows < 1:
    print("Please enter a positive integer.")
else:
    for i in range(1, rows + 1):
        for j in range(1, i + 1):
            print(i + j - 1, end=" ")
        print()

How It Works

1. Read and validate. Parse the answer; reject non-positive values before looping.

2. Same core. i + j - 1 matches Example 1 — only rows comes from the user.

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

Safer input
raw = input("Enter rows: ").strip()
try:
    rows = int(raw)
except ValueError:
    print("Please enter a positive integer.")
    raise SystemExit(1)
if rows < 1:
    print("Please enter a positive integer.")
    raise SystemExit(1)

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm the formula on paper.

Python
rows = 3

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

How It Works

1. Three rows. i = 1 → 1; i = 2 → 2 3; i = 3 → 3 4 5.

2. Trace on paper. If you use i + j instead of i + j - 1, every value shifts up by one.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i + j

Missing −1

Using i + j or 9 + i + j shifts every value — rows no longer start at the row number. Keep i + j - 1.

j <= rows

Rectangle instead of triangle

Inner bound range(1, rows + 1) makes every row the same width. Use range(1, i + 1).

no space

Glued numbers

Without end=" ", values run together (23). Always print a trailing space.

print() inside

Broken rows

If print() sits inside the inner loop, you get one number per line. Call it only after the loop.

rows = 1

Single value

Output is just 1. A good sanity check for input validation.

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)

Row i prints i numbers. Total = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Formula: each cell is i + j - 1 — row i starts at i.
  • Growing width: inner loop runs j = 1..i so row i has i numbers.
  • end= vs print(): values with spaces stay on the line; print() advances after each row.
  • Complexity: O(n²) time from n(n+1)/2 prints; O(1) extra space.

One line: for each row i, print i + j - 1 for j = 1..i with spaces, then print().

Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
print(i + j - 1, end=" ") keeps values separated on the same row. print() ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
Only one row prints — a single 1.

Did you know?

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Next: Increasing Triangle from 0

Continue with the same shape using i + j with i starting at 0.

Program 34 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