C++ Alternating Number Triangle Pattern (Zigzag)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Outer loop sets row length i (counting down). An if/else on i % 2 chooses ascending or descending digits.

MethodIdeaBest for
Parity if/elseOdd → 1..i; even → i..1Learning, interviews, exams
Start / end / stepOne loop with start, end, step from parityLess 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

GoalPattern
Pick row lengthfor (i = rows; i >= 1; i--)
Odd → ascendingfor (j = 1; j <= i; j++) cout << j;
Even → descendingfor (j = i; j >= 1; j--) cout << j;
Choose branchif (i % 2 == 0) { ... } else { ... }
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print digits without a newline, then end the row once.

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 result rows = 5 · 15 digits
12345
4321
123
21
1

Worked Walkthrough — rows = 5

Trace how parity flips the print direction while i shrinks.

iParityDirectionPrints
5odd1..512345
4even4..14321
3odd1..3123
2even2..121
1odd1..11

Total digits = n(n+1)/2 → O(n²). The key is printing j (the sequence), not i.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Odd-Length Descending Pyramid

Continue with rows that skip even lengths (1234567, 12345, 123, 1).

Program 14 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful