C Program to Print 0-01-010-0101 Number Pattern

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

C Source Code: 0-01-010-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 generating related numbers for patterns. printf("\n") takes control of execution to the new line.


/* Program to print 1-01-010-0101 */
#include<stdio.h>

/* Main function */
int main()
{
    int i, j, n;
    printf("Enter number of lines of pattern: ");
    scanf("%d", &n);

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d", (j+1)%2);
        }
        printf("\n");
    }

    return 0;
}

The output of the above program is:

Enter number of lines of pattern: 16

0
01
010
0101
01010
010101
0101010
01010101
010101010
0101010101
01010101010
010101010101
0101010101010
01010101010101
010101010101010
0101010101010101