An alternating number triangle fills rows with a continuous counter. Odd rows print left-to-right; even rows print right-to-left — creating a zig-zag.
Remember
Rule: next starts at 1 (never reset)
odd i → print next ascending
even i → print end..next descending
end = next + i - 1
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15 ← rows = 5
Follows the mixed number triangle in Program 50; next is the increasing-decreasing pyramid in Program 52.
Approach
How to Solve It
Keep a running next. On each row compute end = next + i - 1, then branch on i % 2 for print direction. Increment next after every value.
Method
Idea
Best for
Counter + direction
Odd: print next; even: print end--; always next++
Learning, interviews, exams
Compact trace
Dry-run with rows = 3 before coding rows = 5
Paper tracing and labs
Pseudocode
Pseudocode
next = 1
for i from 1 to rows:
end = next + i - 1
for j from 1 to i:
if i is odd:
print next and a space
else:
print end and a space
end = end - 1
next = next + 1
print newline
Cheat sheet
Goal
Pattern
Pick each row
for (i = 1; i <= rows; i++)
Row width
for (j = 1; j <= i; j++)
Reverse start
end = next + i - 1;
Odd row
printf("%d ", next);
Even row
printf("%d ", end--);
Advance counter
next++; every cell
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d ", value)
Stays on the same line
Each number on the row
printf("\n")
Ends the current line
After the inner loop
Print all values without a newline, then end the row once.
Try it
Live Preview
Change the row count and the zig-zag triangle updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 values
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
Trace
Worked Walkthrough — Row i = 2 (even)
After row 1 printed 1, next is 2. Trace the reverse print for two cells.
Step
State
Prints
Start of row
next = 2, end = 2 + 2 - 1 = 3
—
j = 1
even → print end--
3
j = 2
even → print end--
2
After row
next = 4 (ready for row 3)
3 2
Full row: 3 2. For n rows, total values = n(n+1)/2 → O(n²).
Code
C Programs
Three complete programs: fixed rows = 5, scanf input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — odd rows print next ascending, even rows print end-- descending.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j, next = 1, end;
for (i = 1; i <= rows; i++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
printf("%d ", next);
else
printf("%d ", end--);
next++;
}
printf("\n");
}
return 0;
}
Output
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
How It Works
1. Continuous counter.next starts at 1 and advances once per printed value — never reset between rows.
3. Even rows. Set end = next + i - 1 and print end-- (row 2: 3 2; row 4: 10 9 8 7).
Example 2 — User Input Rows
Read rows with scanf and reject non-positive values.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j, next = 1, end;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
printf("%d ", next);
else
printf("%d ", end--);
next++;
}
printf("\n");
}
return 0;
}
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. Prompt and validate. Reject bad input with a clear message.
2. Same core. Only the outer loop limit comes from the user — counter and odd/even logic stay identical.
3. Safer input tip. Cap demos for readable console output:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — Compact rows = 3
Same counter and odd/even logic with a smaller height for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int rows = 3;
int i, j, next = 1, end;
for (i = 1; i <= rows; i++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
printf("%d ", next);
else
printf("%d ", end--);
next++;
}
printf("\n");
}
return 0;
}
Output
1
3 2
4 5 6
How It Works
1. Three rows. Odd → 1; even → 3 2; odd → 4 5 6.
2. Trace on paper. Confirm next never resets and end is recalculated each even row.
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.
reset next
Broken continuity
If you reset next = 1 each row, numbering restarts and the zig-zag collapses. Keep one counter for the whole triangle.
wrong end
Even row starts wrong
Use end = next + i - 1. Using just next + i or forgetting to set end prints the wrong reverse sequence.
skip next++
Repeated values
If you forget next++ on even rows, later rows reuse numbers. Increment on every cell, both directions.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each value lands on its own line. Call the newline only after the inner loop.
rows = 1
Single row
Output is just 1 — a good sanity check for input validation.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Total values = 1 + 2 + … + n = n(n+1)/2. For n = 5 that is 15 prints.
Remember
Key Takeaways
One counter:next advances across the whole triangle — never reset per row.
Odd / even: odd rows print next; even rows print end-- with end = next + i - 1.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from n(n+1)/2 values; O(1) extra space.
One line: keep next continuous; odd print ascending, even print end--, then printf("\n").
Frequently Asked Questions
Row 2 is an even row, so it prints in reverse order. The row contains numbers 2 and 3, but they are printed as 3 2 using end--.
Even rows print right-to-left to create the alternating effect. We compute end = next + i - 1 and decrement while printing.
We use a running counter next that increments once per printed value. It is not reset between rows, so numbering continues across the whole triangle.
Before printing row i, the last number is next + i - 1. That gives the correct starting point when printing the row in reverse.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because the total printed numbers are 1+2+...+n = n(n+1)/2.
Program 50 concatenates fixed digit sequences per row. Program 51 uses a continuous counter and alternates print direction on odd/even rows.
One row prints 1 — a single ascending value on the first odd row.
🤔
Did you know?
Numbers 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.