Sunday, June 16, 2013

Number Pyramid/Triangle

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

0
01
012
0123
01234
0123
012
01
0

Ans.

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

The output of above program would be:

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