9.05.2013

Equilateral Triangle Number Design

Q. Write a C program to print the following equilateral triangle number design as:

     5
    454
   34543
  2345432
 123454321

Ans.

/*c program for equilateral triangle design*/
#include<stdio.h>
int main()
{

 int num,r,c,sp,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(n=num-r+1; n<=num; n++)
     printf("%d",n);
  for(c=1,n=num-1; c<r; c++,n--)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Equilateral Triangle design C program
Figure: Screen shot for Equilateral Triangle design C program


No comments:

Post a Comment