C Program to Print 0-010-01010 Pyramid Number Pattern

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

C Source Code: 0-010-01010 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 0-010-01010 */
#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+1)%2);
        }
        printf("\n");
    }

    return 0;
}

The output of the above program is:

Enter number of lines of pattern: 10

         0
        010
       01010
      0101010
     010101010
    01010101010
   0101010101010
  010101010101010
 01010101010101010
0101010101010101010