Pyramid triangle

Q. Write a C program for following structure:

9
0 1
2 3 4
5 6 7 8
9 0 1 2 3

Ans.

/*c program for triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,c,r,x=0,n=4;
 printf("9\n");
 for(r=1; n>=r; r++)
 {
  for(c=0; c<=r; c++,x++)
  {
   if(x<10)
     printf("%d ",x);
   else
   {
     for(i=0; i<=3; i++)
        printf("%d ",i);
     break;
   }
  }
  printf("\n");
 }
 getch();
 return 0;
}

/****************Output******************/
Triangle Structure C program
Screen shot for triangle structure C program

Comments