Monday, 17 September 2012

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 454
 34543
 23454321
 123454321

Ans.

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


The output of above program would be:


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

No comments:

Post a Comment