Number triangle

Q. Write a C program to print the following number triangle or structure:

     1
    21
   321
  4321
 54321

Ans.

/*c program code for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(sp=num-r; sp>0; sp--)
      printf(" ");
   for(c=r; c>=1; c--)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

/****************Output**************/

Output of C program for number triangle
Screen shot for number triangle C program

Comments