CS50 pset-1 itsa Mario - make a half-pyramid using parameters from user input I'm taking Harvard's introduction to computer science (CS50) course online, and I've written a program that satisfies a coding challenge but I don't understand what I did or how I can improve it. I'm a very green programmer.

The challenge is to get an int from the user between 0 and 23 and use that to set the line height of a half-pyramid drawn in the terminal using hashes, ala the ramp at the ends of the levels in Super Mario Bros:


   ##
  ###
 ####
#####



After about 6 hours of banging my head against the wall I came up with the following:

#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;

int main()
{
    int height;

    printf("How high do you want your half-pyramid?\n");
    height = GetInt();

     //verify that height is an int between 0 and 23

    while (height &gt; 23 || height &lt; 0) 

        {
            printf("Invalid entry:\nChoose a number between 0 and 23\n");
            height = GetInt();
        }

    // draw the pyramid

    for (int line = 0; line &lt; height; line++)
      {
         // draw spaces
        for (int space = line + 2; space &lt; height + 1; space++)
         {
         printf(" ");
         }

         // draw hashes
        for (int hash = line + height + 1; height &lt; hash + 1; hash--)
        {
         printf("#");
        } 
        printf("\n");
      }

    return 0;
}


I understand the part that I've commented as "verify that height is an int between 0 and 23," but everything in the "draw the pyramid" section was arrived at by brute force. Just making one thing that kind of looked like it worked and then tweaking it over and over again until it did what I needed it to do. Maybe this is how everyone codes, but I hope not.

Can anyone give me some insight in fairly plain language about what the code here is doing, some ideas about how it might be improved, or just some other ways to go about it?
Organized coding

A line of a pyramid is made by x spaces and y hashes. The number of spaces is given by the total line length minus the number of hashes. The number of hashes increases from 2 to 5, the max line length is 5

static int LINE_LEN = 5;
for (int hashes = 2; hashes &lt;= LINE_LEN; hashes++) {
    print_repeated_char(' ', LINE_LEN - hashes);
    print_repeated_char('#', hashes);
    printf("\n");
 }


Also note the print_repeated_char function avoids repetition.