Python Number Triangle Pattern (Alternating Direction)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An alternating number triangle prints continuous integers across rows: odd rows go left-to-right, even rows go right-to-left. With rows = 5 you get 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15.
Unlike Program 50 (fixed digit halves), this shape uses a running counter and flips direction with i % 2.
Approach
How to Solve It
Keep a counter nxt. On each row compute end, then print ascending or descending based on odd/even i.
Method
Idea
Best for
Counter + direction
nxt += 1 always; print nxt or end by parity
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
nxt = 1
for i from 1 to rows:
end = nxt + i - 1
for j from 1 to i:
if i is odd:
print nxt
else:
print end
end = end - 1
nxt = nxt + 1
print newline
Cheat sheet
Goal
Pattern
Start counter
nxt = 1
Row end value
end = nxt + i - 1
Odd row
print(nxt, end=" ")
Even row
print(end, end=" "); end -= 1
Advance always
nxt += 1 after each print
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(n, end=" ")
Stays on the same line
Each value
print()
Ends the current line
After the inner loop
Print values without a newline, then end the row once. Putting bare print() inside the inner loop turns the triangle into a vertical list.
Try it
Live Preview
Change the row count and the alternating triangle updates instantly.
Use rows from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 values
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
Trace
Worked Walkthrough
Trace the first four rows — watch nxt, end, and direction.
Row i
Parity
nxt start
end
Printed
1
odd / asc
1
1
1
2
even / desc
2
3
3 2
3
odd / asc
4
6
4 5 6
4
even / desc
7
10
10 9 8 7
Even on even rows, nxt still increments once per cell — so numbering stays continuous for the next row.
Code
Python Programs
Three complete programs: fixed rows = 5, input() rows, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — odd rows print nxt ascending, even rows print end descending.
Python
rows = 5
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for j in range(1, i + 1):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print()
Output
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
How It Works
1. Compute the row’s last value.end = nxt + i - 1 is the reverse start for even rows.
2. Branch on parity. Odd rows print nxt; even rows print end then end -= 1.
3. Always advance nxt. Increment after every cell so the next row continues the sequence, then end the line with bare print().
Example 2 — User Input Rows
Read the row count at runtime — counter and direction logic stay the same.
Python
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive integer.")
raise SystemExit(1)
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for j in range(1, i + 1):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print()
Output (when user enters 4)
Enter the number of rows: 4
1
3 2
4 5 6
10 9 8 7
How It Works
1. Same counter core. Only the source of rows changes from a literal to input().
2. Entering 4 stops early. You get four rows ending at 10 9 8 7.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if rows < 1 or rows > 9:
print("Enter a whole number from 1 to 9.")
raise SystemExit(1)
Example 3 — Compact rows = 3
Smaller height for quick tracing of nxt, end, and odd/even branches.
Python
rows = 3
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for j in range(1, i + 1):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print()
Output
1
3 2
4 5 6
How It Works
1. Only three rows. Easy to dry-run every increment and both parity branches on paper.
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. One even row in the middle. Row 2 is the only reverse print — the zig-zag is obvious.
Edge Cases & Pitfalls
Check these before calling the solution done.
Forget end
Compute end before the inner loop
Even rows need end = nxt + i - 1 as the reverse start. Skipping it breaks the zig-zag.
nxt += 1
Always increment nxt
Even when printing end, still call nxt += 1 so the next row continues correctly.
Newline
Do not put print() inside the inner loop
Call bare print() only after all values on the row are printed.
Reset nxt
Do not reset the counter per row
Putting nxt = 1 inside the outer loop restarts every row and breaks continuity.
rows = 1
Single value
Output is just 1. A good sanity check for input validation.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: continuous nxt; odd rows ascending, even rows end descending.
Set end:end = nxt + i - 1 before printing an even row.
Break the row: call bare print() only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, print i continuous numbers — left-to-right if odd, right-to-left if even — then end the line.
Frequently Asked Questions
Row 2 is even, so it prints in reverse. The row contains numbers 2 and 3, but they are printed as 3 2 using end -= 1 after each print.
Even rows print right-to-left for the zig-zag effect. We compute end = nxt + i - 1 and decrement while printing.
A running counter nxt increments once per printed value and is never reset between rows.
Program 50 concatenates fixed digit sequences per row. Program 51 uses a continuous counter and alternates print direction on odd/even rows.
Printing a number with end=" " stays on the same line. Bare print() ends the current row. Values use end=" "; the row break uses print() after the inner loop.
O(n²) for n rows because total printed numbers are 1+2+…+n = n(n+1)/2.
Before printing row i, the last number is nxt + i - 1 — the reverse start when the row is even.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
🤔
Did you know?
Numbers stay continuous via a running counter nxt. Odd rows print ascending; even rows print descending using end = nxt + i - 1. Total prints = n(n+1)/2 — time O(n²).