Monday, 3 December 2012

Number Pyramid

Q. Write a C program to print the following number pyramid C program:

1234
341
12
3

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num=4,n,c,r,t;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  if(r==2 || r==4)
  {
    for(c=1,t=3; c<=n; c++,t++)
    {
      if(c==3 && r==2)
         printf("1");
      else
         printf("%d",t);
    }
  }
  else
  {
    for(c=1; c<=n; c++)
        printf("%d", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number pyramid C program
Figure: Screen shot for number pyramid C program

No comments:

Post a Comment