C Program to Print 1-01-010-1010 Binary Number Pattern

Question: write a program in C to generate a 1-01-010-1010-10101 binary number pattern up to n lines, where n is given by the user.

C Source Code: 1-01-010-1010 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 1-01-010-1010 */
#include<stdio.h>

/* Main function */
int main()
{
    int i, j, n, a;
    printf("Enter number of lines of pattern: ");
    scanf("%d", &n);
    a = 1;
    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: 10
1
01
010
1010
10101
010101
0101010
10101010
101010101
0101010101