Column-Wise Alternating Binary Pattern in C

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

What You’ll Learn

How to print a column-wise alternating binary number pattern in C. Unlike the checkerboard binary triangle, this one depends only on the column position.

The rule is simple: print j % 2 in the inner loop.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
10
101
1010
10101
1

Complete C Program

Since j starts at 1 for every row, the first digit is always 1, then it alternates.

c
#include <stdio.h>

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

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

    return 0;
}
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 = 1; i <= rows; ++i) {
        for (j = 1; j <= i; ++j) {
            printf("%d", j % 2);
        }
        printf("\n");
    }

    return 0;
}

Explore More Patterns

Try starting with 0 by printing (j + 1) % 2, or swap digits with characters like X/O.

All Number Patterns →
Did you know?

Even though the pattern looks different from Program 15, both rely on the same idea: use modulo arithmetic to decide what to print at each position.

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