How to make the pyramid aligned to right in mario pset ? How to make the triangle top 2 spaces wide ?

My code so far.

       #include <stdio.h>
       void  main()
       int rows;
       int i;

        printf("Enter the number of rows: ");

        for (i=1,i>rows, i++;)

        printf(" ");



3 Answers 3


Hello Jesvant Somepalli, I would highly recommend that you should watch the walkthrough again, if you have watched already, while taking notes. Use a pen and a paper first to draw the pyramid, while working in your head, how on this planet earth, you would try to instruct the computer to do the same. Nevertheless, I will try to the best of my ability to explain to you on how to go about this problem.

As per the requirement, we want to start off by printing 2 # on the top most row, and keep on increasing 1 # as we move down the height(y-axis). So how will we print the the first row, given that we want to print h height half-pyramid? (If you take a careful look, you will notice that if the height is h then the width would be h+1, lets assign the width its own letter, w). So the very first line would be printed as follows:

print a space w-2 times;
print # 2 times;
print a new line; //to go to the next row

You would notice that for each space and for each # to be printed on each row, you would need a loop, because a space-character would keep on decreasing in number and a # character would keep on increasing as you move down the height(y-axis). So the above code will only and ONLY get printed ONCE, for it to be printed h times we need it to copy and paste it h times. Of-course that would be foolish of us, knowing that we now have the loops in our spell-book.

Woah! that was alot, I don't have a habit to speak alot :p. Anyways, if you find this confusing, then please try to read it again while taking notes, and InshaAllah (if Allah wills) you will find it easy to comprehend.

Hope this helps!


I solved this by using another variable in addition to height. I used numHashes and initialized that to 2. Then, when I went through the loop, I didn't have to worry about alignment, I just printed out the right number of blank spaces and the right number of hashes.

It appears as though you are not taking advantage of the libraries provided by the class, I recommend watching the videos and use the get_integer function to capture input.


In order to get the pyramid to align correctly, you need the requisite number of spaces before the hash marks in order to shift it to the right. I found it helpful to use a different, visible character when I was testing and then to replace it with the space character when I was done. Ex:


Since your for loop currently only prints a single space and a single hash mark each time it runs though, and you know that on every line you will need more than one space and/or more than one hash, it is helpful to think about how to create conditions that cause the printing of those characters to loop a certain number of times as well. Nested for loops helped me with this, though while loops and many other strategies can also work. Ex:

For (i=value; i>input modifier;i++)
    For (relationship between input, hashes, spaces)
      printf spaces
    For (relationship between input, hashes, spaces)
      printf hashes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .