I manage to put the base of the pyramid and the rows needed, but I fail to put hashes before the base.

#include <cs50.h>

int main(void)
    printf("Height: ");
    int Height;
        Height = get_int();
    while (Height < 0 || Height > 23);

    int hashes, spaces, rows;

    for (spaces = 0; spaces > Height; spaces--)
        printf(" ");
    for (rows = Height; rows <= Height; rows++)
     for (hashes = 0; hashes < rows; hashes++)

1 Answer 1


Here is a pseudocode for this program:

For 0 times print spaces
For 1 time print newline
For Height + 1 times print hash

  • For 0 times print spaces: for (spaces = 0; spaces > Height; spaces--). This will run as long as spaces is greater than Height. Since spaces is initialized to 0 , and Height must be greater-equal to 0, spaces is never greater than Height and this loop exits immediately.
  • For 1 time print newline: rows is initialized to Height, so it is less-equal Height and prints a newline. On the next iteration, row is incremented by 1; it is now greater than Height. Loop exits.
  • For Height + 1 times print hash: row has the value Height + 1 when the previous loop is finished, so this loop will print Height + 1 hashes.

pseudocode for the assignment can be seen around 7:05 in the walkthrough:

For height times
    Print some spaces
    Print some hashes
    Print a newline

Of course, the hard part might be figuring out "some" :)

PS an easy way to format code: after you paste it, select it, then click the {} icon in the format bar. Ta-da!

You must log in to answer this question.

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