C program pattern 10101 10101 10101 10101 10101

In this article, we are going to understand and implement an interesting C pattern program, in which, we are going to create the following simple pattern –

10101
10101
10101
10101
10101

If you first see the pattern, there is a probability that you might scratch your head, but with some basic knowledge of C language, and applying simple logic for this pattern, you can easily create a program for this pattern.

If you observe the pattern, we are having 5 rows and 5 columns(you can take them as user input, but we will keep the number of rows and columns the same). Also, in the odd column(1, 3, and 5) we have “1”, and in the even column(2, and 4) we have “0”. So, following this, it becomes very easy to create such a pattern.
In the given video, you can find an easy explanation, along with a program for the given pattern.

Here is the program to implement this simple pattern (with row number as user input) –

#include <stdio.h>

int main() {
int rows;

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

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows; j++) {
if (j % 2 == 0) {
printf("0");
} else {
printf("1");
}
}
printf("n");
}

return 0;
}

Program output 1 – 

Enter the number of rows: 5
10101
10101
10101
10101
10101

Program Output 2 – 

Enter the number of rows: 7
1010101
1010101
1010101
1010101
1010101
1010101
1010101