I can ask for and get user input. I believe the best way to handle the rest is nested for loops where the 1st gives the lines with user input, and the other 2 give the spaces and hashes, that's where things go dark for me. //n is user input

    for (int height =n; height >=0; height--)
        for (int spaces = n; spaces > 0; spaces--)
            printf(" ");

            for (int hashes = 0; hashes <=n; hashes++)

I have tried many variations of this with no success; i understand what the hashes and spaces should do as I move down the lines but cannot make that happen. What am I missing? please help. I have searched but could not find an answer to my specific question.

1 Answer 1


Before you write the code, you need to understand exactly what it needs to do. Start small. If n=1, what is it supposed to do? Print two #'s on a single line. What about n=2? I'll use a * for spaces for clarity, so it's supposed to print 2 lines, *## on one line and `### on the next. Larger values for n produce similar changes. So what's the relationship between n, the number of lines, and the number of spaces and #s on a given line?

You're right about the for loops, but your code isn't structured right, or complete (assuming you didn't copy all of the curly braces.) You have 3 loops, each nested inside the one above. That will cause the # loop to execute n times for each line. The third loop should not be nested inside the second. Restructure them like so:

for( loop1 )
    for( loop 2 )
        // print spaces

        // print hashes

Next, you are controlling the inner loops with n and only n. By doing that, every time they run, they will produce the same line. You need to control the inner loops based on which line is being printed, not the number of lines to print.

Finally, try walking through your code on paper and track what it will do, line by line. That will often help you understand the code and find errors.

That should guide you in the right direction. I could fix it for you, but that would rob you of all the fun of figuring it out!

If this answers your question, please accept the answer to close your question. Let's keep up on forum housekeeping. ;-)

  • thanks for the help, especially about the error in loop code. I will try to work through this but feel like I've tried everything already....although considering the loop code error, things may end up differently. And thanks for not solving it for me, I want to learn so that I will know the answers down the line not just for now.....thanks again!
    – IDFC
    Jun 19, 2015 at 2:01

You must log in to answer this question.

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