9.03.2011

16. Design numbers tringle pyramid

Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

1 2 3 4 5

1 2 3 4


1 2 3



1 2




1

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

12345

1234


123



12




1

********************************/

4 comments:

  1. how can i make it the output like this?

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


    that position is not right im seeking for \ like this position

    ReplyDelete
  2. Hi Emann, code is written below :
    for (int i = 1; i <= 5; i++)
    {
    for (int k = 1; k <= 5; k++)
    {
    for (int j = k; j <= 5; j++)

    printf("%d", j);

    printf("\n");
    }

    ReplyDelete
    Replies
    1. i think this is easier
      #include
      main()
      {
      int i,j;
      for(i=1;i<=5;i++)
      {
      for(j=i;j<=5;j++)
      printf("%d",j);
      printf("\n");
      }
      }

      Delete
  3. 12345
    -1234
    --123
    ---12
    ----1

    #include
    int main()
    {
    int i,j;
    for(i=1; i<=5; i++)
    {
    for(j=1;j<i;j++)
    {
    printf("-");
    }
    for(j=1;j<=6-i;j++)
    {

    printf("%d",j);

    }
    printf("\n");

    }

    return 0;
    }

    ReplyDelete