9.01.2011

2. Design triangle pyramid

Q. Write a program to generate a following #'s triangle?

# # # # #

# # # #


# # #



# #




#

Ans.

/* c program for symbol triangle*/
#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("#");
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5
#####
 ####
  ###
   ##
    #
***********************************/

10 comments:

  1. sir please tell me about the program in details with hint i am unable to understand .

    ReplyDelete
    Replies
    1. Kindly describe what you want to know in above program?

      Delete
  2. i think the variable r is not needed here; y r u using that.

    ReplyDelete
    Replies
    1. @saranya,
      variable r is useful for calculate the white/blank space, so the courser goes to right position.
      Without 'r' variable, the space laps and output may be differ.

      Delete
  3. could you please explain how the program is creating the space ?/
    I am struggling with this..

    ReplyDelete
  4. Just not able to understand pyramids and triangles in C. can anyone recommend a book

    ReplyDelete
  5. Too difficult to understand..can anyone explain?

    ReplyDelete
  6. for(sp=r; sp>1; sp--)
    printf(" ");

    ReplyDelete
  7. How can you make a pyramid in HTML using jsp

    ReplyDelete
  8. #include
    main()
    {
    int i,j,k,n;
    printf("Enter the number of rows");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    for(k=1;k=i;j--)
    {
    printf("@");
    }

    printf("\n");
    }
    }

    ReplyDelete