C Program to Print 1-10-101-1010 Number Pattern

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

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


/* Program to print 1-10-101-1010 */
#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%2);
        }
        printf("\n");
    }

    return 0;
}

The output of the above program is:

Enter number of lines of pattern: 18

1
10
101
1010
10101
101010
1010101
10101010
101010101
1010101010
10101010101
101010101010
1010101010101
10101010101010
101010101010101
1010101010101010
10101010101010101
101010101010101010