7.07.2012

Number triangle

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

     1
    21
   321
  4321
 54321

Ans.

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

/****************Output**************/

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

10 comments:

  1. sir how can we do this
    *****5
    ****54
    ***543
    **5432
    *54321

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int n,r,c,s;
      clrscr();
      printf("enter loop repeat number");
      scanf("%d",&n);
      for(r=n;r>=1;r--)
      {
      for(s=1;s<=r;s++)
      printf("*");
      for(c=n;c>=r;c--)
      printf("%d",c);
      printf("\n");
      }
      getch();
      }

      Delete
  2. 1
    2 1 2
    3 2 1 2 3
    4 3 2 12 3 4 iwant java code for this triangle

    ReplyDelete
  3. ****1
    ***21
    **321
    *4321
    54321
    i want in php code

    ReplyDelete
  4. wat is the c code for_
    1
    21
    321
    4321
    54321

    ReplyDelete
    Replies
    1. @ Saks#!

      Your required number triangle pyramid C program source code at:

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


      Delete
  5. 5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1

    ReplyDelete
    Replies
    1. @Sagar Kacharia,

      Number Triangle Pyramid Pattern C program source code :

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

      Delete
  6. could you do this?
    01234
    10123
    21012
    32101
    43210

    ReplyDelete
    Replies
    1. #Alit

      Thanks for comments, your required number triangle C program source code as:

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

      /***** OUTPUT******/
      01234
      10123
      21012
      32101
      43210

      Delete