Alternating Binary Triangle in C

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
101011
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.
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.
12 people found this page helpful
