Number Pyramid

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

1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Ans.

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

The output of above program would be:

Output of number pyramid C program
Figure: Screenshot for number pyramid C program


Comments