3.29.2013

Static Number Pyramid

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

    1
   232
  34543

Ans.

/*static number pyramid*/
#include<stdio.h>
int main()
{
 int num=3,r,c,x,z,k,sp;
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>0; sp--)
     printf(" ");
  for(c=1,z=r; c<=r; c++,z++)
     printf("%d",z);
  for(k=2,x=r+1; k<=r; x--,k++)
     printf("%d",x);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

    1

   232
  34543

1 comment:

  1. There is an error in your 3rd for loop, plz check it out.

    ReplyDelete