10.29.2012

Number Pyramid

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

54321
5432
543
54
5

Ans.

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

The output of above program would be:

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

11 comments:

  1. Replies
    1. @Toki Tahmid Inan,

      Your required Number Pyramid Source Code at:

      #include"stdio.h"
      int main()
      {
      int num,r,c,n;
      printf("Enter Any Number : ");
      scanf("%d", &num);
      for(r=1; r<=num; r++)
      {
      for(c=1,n=num; c<=r; c++,n--)
      printf("%d", n);
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
  2. Replies
    1. @Shouvik Das,

      Your required number pyramid pattern source code as:

      #include"stdio.h"
      int main()
      {
      int num=5,r,c,p;
      for(r=1; r<=num; r++)
      {
      for(c=r,p=num; c>=1; c--,p--)
      printf("%d",p);
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
  3. By using while loop
    54321
    5432
    543
    54
    5

    ReplyDelete
    Replies
    1. @ Poojitha Pinninti

      Your required number pyramid using while loop source code in C as following:

      #include"stdio.h"
      #include"conio.h"
      int main()
      {
      int num=5,r,c,p;
      r=1;
      while(r<=num)
      {
      p=num;
      c=r;
      while(c<=num)
      {
      printf("%d",p);
      p--;
      c++;
      }
      r++;
      printf("\n");
      }

      getch();
      return 0;
      }

      Delete
  4. Replies
    1. @ Milan Joshi

      The C program source code for above Number Triangle Pyramid program here:

      http://cprogrammingcodes.blogspot.com/2011/09/10design-numbers-tringle-pyramid.html

      Delete
  5. 4
    43
    432
    4321
    how to generate the above pattern

    ReplyDelete
    Replies
    1. #Ritu Naha,

      Your required number pattern C program source code as:

      #include"stdio.h"
      int main()
      {
      int num=4,r,c,p;
      for(r=1; r<=num; r++)
      {
      for(c=r,p=num; c>=1; c--,p--)
      printf("%d",p);
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
    2. How to code the output below in c using while loop
      4321
      321
      21

      Delete