Shape Rule
Alternate direction
Row i prints i numbers — ascending on odd rows, descending on even rows.

Program 51 prints an alternating number triangle: odd rows go left-to-right, even rows go right-to-left, with a continuous counter across all rows — a natural step after Program 50’s mixed number triangle. This tutorial covers running counters, odd/even row logic, a live preview, worked Python examples, edge cases, and complexity.
Alternate direction
Row i prints i numbers — ascending on odd rows, descending on even rows.
next
nxt starts at 1 and increments once per printed value — never reset between rows.
end = next + i - 1
Compute end before each row — the last number that belongs on the current line.
i % 2
Odd rows print nxt; even rows print end -= 1 for the zig-zag effect.
rows = 3..9
Pick row count and draw the alternating number triangle in the browser.
Complexity
Total prints = 1+2+…+n = n(n+1)/2 — classic triangular growth.
An alternating number triangle pattern prints row i with i continuous numbers — ascending on odd rows, descending on even rows. With rows = 5, you get 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15.
In Python, a running counter nxt tracks the next value, end = nxt + i - 1 sets the reverse start, and i % 2 picks print direction before print().
It bridges Program 50’s mixed number triangle to zig-zag patterns — combining a running counter with odd/even row direction.
Print nxt ascending left-to-right.
Print end -= 1 descending right-to-left.
Program 50 uses fixed digit halves; Program 51 uses a continuous counter with alternating direction.
Follow Program 50; continue to Program 52 next.
In short: track nxt = 1, compute end = nxt + i - 1, print ascending on odd rows and end -= 1 on even rows, increment nxt each time, then print().
Given row count rows = 5, print an alternating number triangle — row i shows i continuous numbers, alternating direction each row.
# rows = 5
//1
//3 2
//4 5 6
//10 9 8 7
//11 12 13 14 15 | Item | Type | Description |
|---|---|---|
rows | int | How many triangle rows to print. |
i (outer) | int | Current row index — runs from 1 to rows. |
nxt | int | Running counter — next number to assign; increments each print. |
end | int | Last number on the row: next + i - 1; decremented on even rows. |
j (inner) | int | Print loop — runs 1..i values per row. |
| Row length | int | Row i prints exactly i numbers. |
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 -= 1
nxt += 1
print newline | Approach | Idea | Best for |
|---|---|---|
| Odd/even direction | i % 2 picks ascending vs descending print | Learning and interviews |
| Running counter | nxt increments once per printed value | Continuous numbering across rows |
| User-input rows | int(input()) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Spaced variant | print(n, end=" ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Outer loop | for i in range(1, rows + 1): |
| Init counter | nxt = 1 |
| Row end value | end = nxt + i - 1 |
| Odd row print | if i % 2 == 1: print(nxt, end=" ") |
| Even row print | else: print(end, end=" "); end -= 1 |
| Advance counter | nxt += 1 once per printed value |
| Program 50 contrast | Program 50 uses fixed digit halves; Program 51 alternates direction with a running counter |
Same triangle — three ways to set row count and format output.
rows = 5Hard-coded height for demos
int(input())Read row count from console
rows = 3Quick dry-run on paper
nxt = 1Continuous numbering across rows
i % 2Picks ascending vs descending
Reach for this pattern when teaching running counters, odd/even row logic, and zig-zag print direction.
Natural follow-up after Program 50’s mixed number triangle — introduces alternating print direction.
Similar logic appears in matrix serpentine traversals and boustrophedon ordering.
Total prints = n(n+1)/2 — classic nested-loop complexity example.
Compare Program 50 (mixed number triangle) with this alternating pattern, then continue to Program 52.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in running counters, odd/even logic, and O(n²) thinking.
Choose row count between 3 and 9 and draw the alternating number triangle in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print five rows of the alternating number triangle with a running counter and odd/even direction.
rows = 5Hard-coded row count — odd rows print nxt ascending, even rows print end descending.
rows = 5
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for _ in range(i):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print() When i = 2, the row is even: end = 3, so it prints 3 2 in reverse. When i = 1, the odd row prints nxt = 1 ascending.
Read row count with int(input()) and validation.
Read rows with int(input()) and validate the result.
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
else:
if rows <= 0:
print("Please enter a positive integer.")
else:
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for _ in range(i):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print() Same counter and odd/even core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace the counter and odd/even logic quickly before scaling to 5 rows.
rows = 3
nxt = 1
for i in range(1, rows + 1):
end = nxt + i - 1
for _ in range(i):
if i % 2 == 1:
print(nxt, end=" ")
else:
print(end, end=" ")
end -= 1
nxt += 1
print() With only three rows you can trace every nxt increment and odd/even branch on paper before running the full rows = 5 demo.
Set nxt = 1 before the outer loop — it tracks the next number to assign.
end = next + i - 1 — the last number that belongs on row i.
Odd rows print nxt; even rows print end -= 1. Increment nxt each time.
print() after the inner loop finishes each row.
Total prints = 1+2+…+n — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s counter values, print direction, and full line output.
i | nxt at start | end | Direction | Row output |
|---|---|---|---|---|
1 | 1 | 1 | odd / asc | 1 |
2 | 2 | 3 | even / desc | 3 2 |
3 | 4 | 6 | odd / asc | 4 5 6 |
4 | 7 | 10 | even / desc | 10 9 8 7 |
5 | 11 | 15 | odd / asc | 11 12 13 14 15 |
Row i always prints exactly i numbers — the counter never resets between rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Inner bound grows with outer index — classic nested-loop exercise.
Example: trace row i = 4 in the walkthrough table.
Alternating direction mirrors serpentine matrix walks — a common interview pattern.
Example: row 5 ends with 11 12 13 14 15 — five ascending values on an odd row.
Practice print(nxt, end=" ") vs print() with odd/even row direction.
Example: put print() inside the inner loop by mistake.
Total prints = n(n+1)/2 — links loops to summation formulas.
Example: 10 rows print 55 values total.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows = 1+2+3+4+5 = 15 prints.
Pair the pattern with int(input()) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong inner bounds show up immediately as a broken triangle.
Alternating direction with a running counter — bridges loops to zig-zag logic.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 2 on paper — watch end = 3 print 3 2 while nxt advances to 4.
Small habits that keep number-pattern code clean.
Row i prints exactly i values — use for _ in range(i):.
Avoid crashes when the user types letters instead of a number.
Only call print() after the inner loop finishes the row.
Trace rows = 3 on paper before coding the full rows = 5 demo.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put print() inside the inner loop.
Mistakes that commonly break alternating number triangle patterns.
Each number lands on its own line — you get a column, not a triangle.
→ Use print(nxt, end=" ") or print(end, end=" "); print() only after the inner loop.
Using range(rows) every row makes a full rectangle, not a triangle.
→ Use for _ in range(i): — inner bound depends on outer i.
Numbers restart at 1 every line — the continuous sequence breaks.
→ Keep nxt outside the outer loop and only increment it inside the inner loop.
All numbers print on one long line without row breaks.
→ Add print() after each inner loop completes.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Wrap in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Five rows ending with 11 12 13 14 15 — good for dry-runs.
Unchecked int(input()) raises ValueError — use try/except.
Row 9 has 9 numbers — total prints grow as n(n+1)/2.
Try these variations to lock in the pattern.
nxt = 1. Compute end = nxt + i - 1 per row. Never reset nxt between rows.print(..., end=" ") stays on the line; print() advances — call it only after the inner loop finishes.rows > 0 for interactive programs; rows = 1 prints a single 1.1+2+…+n = n(n+1)/2 for n rows — triangular growth, not a full square.Quick Takeaway: init nxt = 1, compute end = nxt + i - 1, print ascending on odd rows and end -= 1 on even rows, increment nxt each time, then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Total prints for n rows | n(n+1)/2 values | i numbers on row i |
The alternating number triangle is a natural follow-up to Program 50: a running counter with odd/even row direction for zig-zag output. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 52 for the next pattern in the series.
Row i prints i continuous numbers — ascending on odd rows, descending on even rows.
nxt = 1 before the outer loopend = nxt + i - 1 at the start of each rowprint(nxt, end=" ")print(end, end=" "); end -= 1nxt += 1 once per printed valuenxt to 1 on every rowend before even rowsprint() inside the inner looprows = 3 dry-run before coding rows = 5Print the pattern the beginner-friendly way.
Odd rows asc, even rows desc
Definitionnxt = 1, never reset
Codeend = next + i - 1
Codei % 2 picks direction
LogicO(n²) time
AnalysisNumbers stay continuous across rows via a running counter nxt. Odd rows print ascending; even rows print descending using end = nxt + i - 1. Row 2 shows 3 2 — still O(n²) total prints for n rows.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful