Home > database >  Print the number 5 as a reverse pattern in C
Print the number 5 as a reverse pattern in C

Time:05-14

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");
}

CodePudding user response:

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.

CodePudding user response:

#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 
  •  Tags:  
  • c
  • Related