javafor-loop

Nested for loop that prints out the numbers backwards?


I've got to used a nested for loop to print out the sequence:

544333222211111

I've written out a nested for loop below but it's printing: 555443321

int i,j;

for(i=5;i>=1;i=i-1)
{
    for(j=1;j<i+1;j+=2)
    {
        System.out.print(i);
    }

}

Can someone tell me how to modify the code above so that it prints 544333222211111 instead?


Solution

  • instead of

    for(j=1;j<i+1;j+=2)
    

    try

    for (j = i; j <= 5; j++)