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 each row while flipping print direction: odd length prints 1..i, even length prints i..1.

Remember
Rule: for i = n..1, odd → 1..i, even → i..1

12345
4321
123
21
1       ← 5 rows

In C you solve it with a descending outer loop plus if (i % 2 == 0): even rows use a descending inner loop, odd rows use ascending, then printf("\n") ends the row. Program 12 is a repeating-digit shrink; this one prints sequential digits and zigzags.

How to Solve It

Outer loop shrinks from rows to 1. Parity picks ascending or descending digits. End each row with printf("\n").

MethodIdeaBest for
If/else + nested loopsOuter shrinks n..1; parity picks ascending or descending jLearning, interviews, exams
Direction variablesOne inner loop with start, end, and stepShorter demos once parity clicks

Pseudocode

Pseudocode
for i from rows down to 1:
    if i is even:
        for j from i down to 1:
            print j (no newline)
    else:
        for j from 1 to i:
            print j (no newline)
    print newline

Cheat sheet

GoalPattern
Shrink each rowfor (i = rows; i >= 1; i--)
Odd → ascendingfor (j = 1; j <= i; j++) printf("%d", j);
Even → descendingfor (j = i; j >= 1; j--) printf("%d", j);
Pick branchif (i % 2 == 0) { ... } else { ... }
End the rowprintf("\n");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j)Stays on the same lineEach digit j
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the zigzag triangle updates instantly — capped at 9 so every digit stays a single character.

Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
12345
4321
123
21
1

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, its parity, and the digits printed.

iParityDirectionPrinted row
4even4..14321
3odd1..3123
2even2..121
1odd1..11

Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.

C Programs

Three complete programs: fixed height, scanf input, and a direction-variables shortcut. Use View Output for sample results.

Example 1 — Fixed rows = 5

Outer loop shrinks the length; i % 2 picks ascending or descending digits.

C
#include <stdio.h>

int main(void)
{
    int rows = 5;
    int i, j;

    for (i = rows; i >= 1; i--)
    {
        if (i % 2 == 0)
        {
            for (j = i; j >= 1; j--)
                printf("%d", j);
        }
        else
        {
            for (j = 1; j <= i; j++)
                printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Shrink. for (i = rows; i >= 1; i--) yields 5, 4, 3, 2, 1 — the length of each row.

2. Flip direction. Odd i prints 1..i (12345); even i prints i..1 (4321).

3. Newline. printf("\n") after the inner loop starts the next shorter zigzag row.

Example 2 — User Input

Read the row count with scanf and reject non-positive values.

C
#include <stdio.h>

int main(void)
{
    int rows;
    int i, j;

    printf("Enter the number of rows: ");
    if (scanf("%d", &rows) != 1 || rows < 1)
    {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    for (i = rows; i >= 1; i--)
    {
        if (i % 2 == 0)
        {
            for (j = i; j >= 1; j--)
                printf("%d", j);
        }
        else
        {
            for (j = 1; j <= i; j++)
                printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Validate rows. Require scanf success and rows >= 1.

2. Same zigzag logic. Four rows start at 4321 (even) and end at 1 (odd).

3. Safer demos. Cap at single-digit rows if you want a tight look:

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 — Direction Variables

Replace the if/else blocks with start, end, and step derived from parity.

C
#include <stdio.h>

int main(void)
{
    int rows = 5;
    int i, j, start, end, step;

    for (i = rows; i >= 1; i--)
    {
        start = (i % 2 == 0) ? i : 1;
        end = (i % 2 == 0) ? 1 : i;
        step = (i % 2 == 0) ? -1 : 1;

        for (j = start; step > 0 ? j <= end : j >= end; j += step)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Encode direction. Even i sets start = i, end = 1, step = -1; odd sets the opposite.

2. One inner loop. The condition step > 0 ? j <= end : j >= end walks either way — same output as Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

swapped parity

Ascending on even rows

That mirrors the zigzag. Keep odd → 1..i and even → i..1 for this lesson’s sample output.

\n inside

printf("\n") inside the inner loop

That puts each digit on its own line. Print digits without a newline; call printf("\n") only after the row finishes.

no shrink

for (i = 1; i <= rows; i++)

Counting up grows the triangle. Keep i = rows; i >= 1; i-- so the first line is longest.

rows = 1

Single digit

Output is just 1 — a good sanity check.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

rows > 9

printf("%d", 10) prints two characters, so columns drift. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
If/else nested loops (Examples 1–2)O(n²)O(1)
Direction variables (Example 3)O(n²)O(1)

There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for i = n..1, odd prints 1..i, even prints i..1.
  • Parity: i % 2 == 0 is the flip switch that creates the zigzag.
  • printf vs newline: digits stay on the line; printf("\n") advances after each row.
  • Next step: Program 14 builds an odd-length descending number triangle.

One line: shrink i from n to 1; use parity to print 1..i or i..1 with printf, then printf("\n").

Frequently Asked Questions

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, creating a zigzag direction.
The second row from the top has length i = 4, which is even. The even branch runs for (j = i; j >= 1; j--) and prints 4, 3, 2, 1.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Remove the if/else and keep only one inner loop — either 1..i for ascending or i..1 for descending on every row.
Program 12 repeats the row digit with for (j = i; j <= rows; j++) (11111, 2222, 333, 44, 5). Program 13 prints sequential digits 1..i or i..1 and alternates direction using i % 2.
O(n²) where n is the number of rows. Total digit prints equal n+(n-1)+…+1 = n(n+1)/2.
Yes. Swap the if and else branches so odd rows descend and even rows ascend — the zigzag still works, just mirrored.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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

Practice another shrink rule with descending digit rows.

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