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 C# examples, edge cases, and complexity.
Alternate direction
Row i prints i numbers — ascending on odd rows, descending on even rows.
next
next 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 next; even rows print end-- 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 C# a running counter next tracks the next value, end = next + i - 1 sets the reverse start, and i % 2 picks print direction before WriteLine().
It bridges Program 50’s mixed number triangle to zig-zag patterns — combining a running counter with odd/even row direction.
Print next ascending left-to-right.
Print end-- 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 next, compute end = next + i - 1, print ascending on odd rows and end-- on even rows, increment next each time, then WriteLine().
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. |
next | 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. |
next = 1
for i from 1 to rows:
end = next + i - 1
for j from 1 to i:
if i is odd: print next
else: print end; end--
next++
print newline | Approach | Idea | Best for |
|---|---|---|
| Odd/even direction | i % 2 picks ascending vs descending print | Learning and interviews |
| Running counter | next increments once per printed value | Continuous numbering across rows |
| User-input rows | int.TryParse(...) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Spaced variant | Console.Write(j + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Init counter | int next = 1; |
| Row end value | int end = next + i - 1; |
| Odd row print | if (i % 2 == 1) Console.Write(next + " "); |
| Even row print | else Console.Write(end-- + " "); |
| Advance counter | next++; 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
TryParseRead row count from console
rows = 3Quick dry-run on paper
next = 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 C# 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 next ascending, even rows print end-- descending.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int next = 1;
for (int i = 1; i <= rows; i++)
{
int end = next + i - 1;
for (int j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
} When i = 2, the row is even: end = 3, so it prints 3 2 in reverse. When i = 1, the odd row prints next = 1 ascending.
Read row count from the console with safe parsing.
Read rows from the console with int.TryParse — reject invalid input gracefully.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
int next = 1;
for (int i = 1; i <= rows; i++)
{
int end = next + i - 1;
for (int j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
} 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.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int next = 1;
for (int i = 1; i <= rows; i++)
{
int end = next + i - 1;
for (int j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
} With only three rows you can trace every next increment and odd/even branch on paper before running the full rows = 5 demo.
Set next = 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 next; even rows print end--. Increment next each time.
Console.WriteLine() 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 | next 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 Write vs WriteLine with multiple values per row.
Example: put WriteLine 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 TryParse 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 next advances to 4.
Small habits that keep number-pattern code clean.
Row i prints exactly i values — use j <= i.
Avoid crashes when the user types letters instead of a number.
Only call WriteLine() 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 WriteLine 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 Console.Write(next + " ") or Console.Write(end-- + " "); WriteLine only after the inner loop.
Using j <= rows every row makes a full rectangle, not a triangle.
→ Use for (j = 1; j <= i; j++) — inner bound depends on outer i.
Numbers restart at 1 every line — the continuous sequence breaks.
→ Keep next outside the outer loop and only increment it inside the inner loop.
All numbers print on one long line without row breaks.
→ Add Console.WriteLine() after each inner loop completes.
Letters or empty input throw FormatException.
→ Prefer int.TryParse 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.
Convert.ToInt32 throws — use TryParse.
Row 9 has 9 numbers — total prints grow as n(n+1)/2.
Try these variations to lock in the pattern.
next = 1. Compute end = next + i - 1 per row. Never reset next between rows.Console.Write stays on the line; WriteLine 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 next = 1, compute end = next + i - 1, print ascending on odd rows and end-- on even rows, increment next each time, then WriteLine().
| 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.
int next = 1 before the outer loopend = next + i - 1 at the start of each rowConsole.Write(next + " ")Console.Write(end-- + " ")next++ once per printed valuenext to 1 on every rowend before even rowsWriteLine inside the inner looprows = 3 dry-run before coding rows = 5Print the pattern the beginner-friendly way.
Odd rows asc, even rows desc
Definitionnext = 1, never reset
Codeend = next + i - 1
Codei % 2 picks direction
LogicO(n²) time
AnalysisNumbers stay continuous across rows via a running counter next. Odd rows print ascending; even rows print descending using end = next + i - 1. Row 2 shows 3 2 — still O(n²) total prints for n rows.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful