[Solved] What is my C code not printing # in staircase pattern, this is from hackerrank, I did not pass all test cases, but i can’t point out why?


The problem is in the condition

if((i + j) > ((n / 2) + 1))

It should be

if(j >= n - i - 1)    // or   if(i + j >= n - 1)

To make this easier, I would create a helper function. Also, there’s no need for the VLA a[n][n] that you don’t even use for anything.

void repeat_char(int x, char ch) {
    for(int i=0; i < x; ++i) putchar(ch);
}

void staircase(int n) {
    for(int i = 1; i <= n; ++i) {        
        repeat_char(n - i, ' ');   // or  printf("%*s", n - i, "");
        repeat_char(i, '#');
        putchar('\n');
    }
}

3

solved What is my C code not printing # in staircase pattern, this is from hackerrank, I did not pass all test cases, but i can’t point out why?