C Program to Print 0-10-101-0101 Binary Number Pattern

Question: write a program in C to generate a 0-10-101-0101 binary number pattern up to n lines, where n is given by the user.

C Source Code: 0-10-101-0101 Pattern

In this program, we first read the value of n from the user and loop n times to control the number of lines using loop control variable i. Loop controlled by j is responsible for controlling numbers for patterns in row. And, variable a is responsible for generating binary number patterns using a%2 and statement printf("\n") takes control of execution to the new line.


/* Program to print 0-10-101-0101 */
#include<stdio.h>

/* Main function */
int main()
{
    int i, j, n, a;
    printf("Enter number of lines of pattern: ");
    scanf("%d", &n);
    a = 0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d", a%2);
            a++;
        }
        printf("\n");
    }

    return 0;
}

The output of the above program is:

Enter number of lines of pattern: 11
0
10
101
0101
01010
101010
1010101
01010101
010101010
1010101010
10101010101