We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

And then I print a number of # amounting to the current loop index on each line followed with a newline break.

But since this will lead to the output being aligned on the left,
I also print (n-i) spaces on each line where n is the maximum number and i is the current loop index before executing (2). So for instance when i is 1, I print 5 spaces before I print 1 # to fill up the six spaces (total number of spaces on each line). When i is 2, I print 4 spaces before I print 2 # to make sure all the six spaces on the line are filled.
This also ensures that the # is aligned to the right.

NB. The range is (1, n+1). I didn't start from 0 because that will cause an empty space at the beginning of the output and I also ended at n+1 because the loop ends and n-1 so I add 1 to get to the correct n.

## Staircase

You are viewing a single comment's thread. Return to all comments →

NB. The range is (1, n+1). I didn't start from 0 because that will cause an empty space at the beginning of the output and I also ended at n+1 because the loop ends and n-1 so I add 1 to get to the correct n.

Do you get it now?

Yes! you explained it awesome. Thank you man!

void staircase(int n) { int i,j; for(i=n;i>0;i--) { for(j=1;j<=n;j++) { if(j>=i) { printf("#"); } else printf(" ");

}

} thankx man