An alternating zigzag number triangle shrinks from rows down to 1, flipping direction by parity: odd length prints 1..i, even length prints i..1.
Remember
Rule: for i from rows down to 1
if i is odd: print 1..i
if i is even: print i..1
12345
4321
123
21
1 ← rows = 5
Follows the shrinking repeating pattern in Program 12; next is the odd-length descending pyramid in Program 14.
Approach
How to Solve It
Outer loop sets row length i (counting down). An if/else on i % 2 chooses ascending or descending digits.
Method
Idea
Best for
Parity if/else
Odd → 1..i; even → i..1
Learning, interviews, exams
Start / end / step
One loop with start, end, step from parity
Less duplicated inner-loop code
Pseudocode
Pseudocode
for i from rows down to 1:
if i is even:
for j from i down to 1: print j
else:
for j from 1 to i: print j
print newline
Cheat sheet
Goal
Pattern
Pick row length
for (i = rows; i >= 1; i--)
Odd → ascending
for (j = 1; j <= i; j++) cout << j;
Even → descending
for (j = i; j >= 1; j--) cout << j;
Choose branch
if (i % 2 == 0) { ... } else { ... }
End of row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j
Stays on the same line
Each digit on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the zigzag triangle updates instantly — capped at 9 for readable single-digit demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
12345
4321
123
21
1
Trace
Worked Walkthrough — rows = 5
Trace how parity flips the print direction while i shrinks.
i
Parity
Direction
Prints
5
odd
1..5
12345
4
even
4..1
4321
3
odd
1..3
123
2
even
2..1
21
1
odd
1..1
1
Total digits = n(n+1)/2 → O(n²). The key is printing j (the sequence), not i.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer shrinks; i % 2 chooses ascending or descending digits.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
if (i % 2 == 0)
{
for (j = i; j >= 1; j--)
cout << j;
}
else
{
for (j = 1; j <= i; j++)
cout << j;
}
cout << "\n";
}
return 0;
}
Output
12345
4321
123
21
1
How It Works
1. Outer sets the length.i starts at 5 and decreases to 1.
2. Parity picks direction. Even i prints i..1; odd i prints 1..i.
3. Newline. Call cout << "\n" only after the chosen inner loop finishes the row.
Example 2 — User Input Rows
Read rows with cin, validate, then run the same nested loops.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter the number of rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = rows; i >= 1; i--)
{
if (i % 2 == 0)
{
for (j = i; j >= 1; j--)
cout << j;
}
else
{
for (j = 1; j <= i; j++)
cout << j;
}
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
4321
123
21
1
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Outer shrink / parity branches match Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable single-digit output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm odd ascending vs even descending.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = rows; i >= 1; i--)
{
if (i % 2 == 0)
{
for (j = i; j >= 1; j--)
cout << j;
}
else
{
for (j = 1; j <= i; j++)
cout << j;
}
cout << "\n";
}
return 0;
}
Output
123
21
1
How It Works
1. Three rows.i = 3 (odd) → 123; i = 2 (even) → 21; i = 1 → 1.
2. Trace on paper. If you forget the even branch, every row becomes ascending and the zigzag disappears.
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.
print i
Repeated digits instead of a sequence
This pattern prints j (the running digit), not i. Printing i gives repeating-digit rows like Programs 9–12.
swap branches
Mirrored zigzag
Swapping the odd/even branches mirrors the pattern (odd descends, even ascends). Either rule works — stay consistent with the sample.
no if
Lost the zigzag
One direction only (always 1..i or always i..1) removes the alternating look. Keep the parity check.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check for input validation.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
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 digits printed = n + (n-1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Shrink the length: outer i = rows..1 sets how many digits belong on the row.
Flip with parity: odd → 1..i; even → i..1 via i % 2.
Print j: the sequence value is j; end the row with cout << "\n".
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: for each length i from rows down to 1, print 1..i or i..1 by parity, then newline.
Frequently Asked Questions
An alternating zigzag triangle: for rows=5 you get 12345, 4321, 123, 21, 1 — odd lengths ascend, even lengths descend.
The program checks whether i is even or odd. For odd i it prints 1..i ascending; for even i it prints i..1 descending.
That row has length i = 4, which is even. The even branch runs for (j = i; j >= 1; j--) and prints 4, 3, 2, 1.
Program 12 repeats the row digit (11111, 2222…). Program 13 prints sequential digits 1..i or i..1 and alternates direction with i % 2.
Remove the if/else and keep only one inner loop — either 1..i for ascending or i..1 for descending on every row.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are n+(n-1)+…+1 = n(n+1)/2.
🤔
Did you know?
Odd row length i prints 1..i; even row length prints i..1. The outer loop shrinks from rows to 1, and i % 2 flips direction — still O(n²) total digit prints.