Column-Wise Alternating Binary Pattern in C

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