11.04.2012

Number Pyramid

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

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

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<r; sp++)
     printf(" ");
  for(c=r; c<=num; 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


2 comments:

  1. i want the output like this..
    123456
    1234
    12
    1
    please help

    ReplyDelete
    Replies
    1. For(i=6;i>=1;i-=2)
      {
      For(j=1;j<=i;j++)
      {
      Printf("%d",j);
      }
      printf("\n");
      }

      Delete