Alternating Row Zigzag Number Pattern in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Conditionals

What You’ll Learn

How to print an alternating row ascending/descending number pattern in C. Rows switch direction based on whether the current row length is odd or even.

This is an excellent pattern to practice nested loops + if/else logic.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
4321
123
21
1
1

Complete C Program

If the row length i is odd, print 1..i. If it’s even, print i..1.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Outer loop controls row length

i counts down from rows to 1. Each iteration prints one line with exactly i digits.

Row length
2

Odd rows print ascending

When i is odd, run j = 1..i to print 123...i.

Ascending
3

Even rows print descending

When i is even, run j = i..1 to print a countdown like 4321.

Descending
=

Zigzag effect

Direction alternates by row parity, producing a zigzag. Total prints remain n(n+1)/2 so time complexity is O(n²).

2

Variation — User Input Version

Accept the number of rows from the user:

c
#include <stdio.h>

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

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Reverse the alternation (even ascending, odd descending)
  • Add spaces for readability: printf("%d ", j)
  • Use a different rule (every 3 rows switch direction)
  • Print only odd numbers or even numbers for a variation

Avoid

  • Duplicating logic with separate variables when one loop variable is enough
  • Forgetting the newline after each row
  • Not validating user input

Key Takeaways

1

Odd row lengths print ascending; even row lengths print descending.

2

i % 2 decides the direction for each row.

3

Total prints are still n(n+1)/2 so the runtime stays O(n²).

4

This pattern demonstrates how small conditionals can create big visual changes.

❓ Frequently Asked Questions

Because we check whether i is odd or even using i % 2, and choose a different inner loop direction.
Yes. Print ascending when i % 2 == 0 and descending when i % 2 != 0.
O(n²) because total prints are 1+2+…+n = n(n+1)/2.

Explore More Zigzag Patterns

Alternating direction is a powerful trick. Try alternating symbols, spaces, or even prime numbers per row.

All Number Patterns →
Did you know?

Zigzag patterns are a common stepping stone to more advanced tasks like snake/spiral matrix printing, because both rely on switching direction based on parity.

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