C Program to Print 1-101-10101 Pyramid Number Pattern

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

C Source Code: 1-101-10101 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 spaces for patterns. Similarly, a loop controlled by k is responsible for generating 0s and 1s for the pyramid pattern. printf(ā€œ\nā€) takes control of execution to the new line.


/* Program to print 1-101-10101 */
#include<stdio.h>

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

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

    return 0;
}

The output of the above program is:

Enter number of lines of pattern: 20

                   1
                  101
                 10101
                1010101
               101010101
              10101010101
             1010101010101
            101010101010101
           10101010101010101
          1010101010101010101
         101010101010101010101
        10101010101010101010101
       1010101010101010101010101
      101010101010101010101010101
     10101010101010101010101010101
    1010101010101010101010101010101
   101010101010101010101010101010101
  10101010101010101010101010101010101
 1010101010101010101010101010101010101
101010101010101010101010101010101010101