:( handles a height of 1 correctly \ expected output, but not " ##\n" :( handles a height of 2 correctly \ expected output, but not " ##\n ###\n"

i am getting above error for mario pyramid problem in cs50 and my code is

#include <cs50.h>
#include <stdio.h>

int main(void){
int h;
 int i;
 int s;
 int d;
 printf("Height: ");
  h = GetInt();
 while(h < 0 || h > 23);

  for (i=0; i<h; i++){
  for (s=0; s<(h-i); s++){printf(" ");}
  for (d=0; d<=(i+1); d++){printf("#");}

 return 0;


i know the problem is one extra space but how to rectify?

1 Answer 1


If you want to remove that extra space, just reduce the number of iterations in your space-printing loop by one. For example

for (s = 0; s < h - i - 1; s++)

would do, as would

for (s = 1; s < h - i; s++)

but that's less obvious in how many iterations it does. Convention is to either start or end at 0, and use < or >, but not <= or >=, so the number of iterations h-i-1 (or i+2 in the hash-printing loop) is immediately visible.

You must log in to answer this question.

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