Alternating Binary Triangle in C

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

What You’ll Learn

How to print an alternating binary number triangle in C. The output alternates between 1 and 0 based on position parity.

The cleanest rule is: print 1 when (i + j) is even, else print 0.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
01
101
0101
10101
1

Complete C Program

Use (i + j) % 2 to decide whether to print 1 or 0.

c
#include <stdio.h>

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

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

    return 0;
}

Explore More Patterns

Try changing the parity rule or printing characters (X/O) instead of 1/0 to create different checkerboard effects.

All Number Patterns →
Did you know?

Rules like (i + j) % 2 are also used in chessboard coloring and matrix parity problems—pattern printing is a fun way to practice them.

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