1.26.2013

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

2 comments:

  1. Hey, Here is an easy one... For any no. of rows:#include
    int main()
    {
    int i,j,rows;
    printf("Enter no. of rows: ");
    scanf("%d",&rows);

    printf("Here's your pattern: \n");
    for (i=1;i<=rows;i++)
    {
    for(j=i;j!=0;)
    {
    if(j%2==0)
    {
    printf("01");
    j=j-2;
    }
    else
    {
    printf("1");
    j--;
    }
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  2. 1
    0 0
    1 1 1
    0 0 0 0

    ai program ta kora jabe plz paele code ta den amar exam ase aj plz
    help any person.

    ReplyDelete