'Print the number 5 as a reverse pattern in C

C As you see in this code I'm trying to print the below pattern, Is the code is right to print the pattern cuz in my computer it show's wrong.

//To print this type of pattern
    //5 5 5 5 5 5 
    //5 5 5 5 5
    //5 5 5 5
    //5 5 5
    //5
int i, j, a=5;
for(i=0; i<=5; i++)
{
    for(j=i; j<=5; j++)
    {
        printf("%d", a);
    }
    printf("\n");
}
c


Solution 1:[1]

#include <stdio.h>

int main(void) {
    int height = 5;
    char line[height*2];
    
    for(int i=0; i<height*2; i+=2)
    {
        line[i]='0'+height;
        line[i+1]=' ';
    }

    for (int i=0; i<height; ++i)
    {
        printf("%.*s\n",2*(height-i), line);
    }
    
    return 0;
}

Result

Success #stdin #stdout 0s 5532KB
5 5 5 5 5 
5 5 5 5 
5 5 5 
5 5 
5 

Solution 2:[2]

A couple mistakes here. First, remember that counting in your loop starts at 0, so from 0 to 5 is actually 6 rounds through your top for loop. What you want is for i to range between [0-4] inclusive. To do this modify the <= loop condition to be <.

Additionally add a space after "%d " if you want spaces between the each 5.

Try running through it with pen and paper, there is another mistake in the j loop as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abelenky
Solution 2 jzimmerman