1 and 0 Pyramid


Q. Write a C program to print the following 1, 0, and 1 number pyramid as:

1
01
101
0101
10101

Ans.

/*c 1 and 0 triangle program*/
#include<stdio.h>
int main()
{
 int r,c,num,v;
 printf("Enter number of rows: ");
 scanf("%d"&num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if( (r%2) == 0 )
    {
      v = (c%2==0) ? 1 : 0 ;
      printf("%d",v);
    }
    else
    {
      v = (c%2==0) ? 0 : 1 ;
      printf("%d",v);
    }
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of 1 and 0 pyramid C program
Figure: Screen shot for 1 and 0 Number pyramid C program

Comments