user13807398
user13807398

Reputation:

Make a reverse number in java

guys. I just make a java program that suppose to display like this:

16 12 8 4 
15 11 7 3
14 10 6 2
13  9 5 1

Here is my current code:

int rows=4, cols=4;
for (int i = rows; i >= 1; i--) {
    for (int j = 0; j < cols; j++) {
        int number = i + (j * rows);
        System.out.print(number + "\t");
    }
    System.out.println();
}

But it just show wrong display:

4 8 12 16
3 7 11 15
2 6 10 14
1 5  9 13

Can you guys tell me, what's wrong with my code? Thank you

Upvotes: 0

Views: 77

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

Change the second loop from increasing to decreasing. That is,

for (int j = 0; j < cols; j++) {

to

for (int j = cols - 1; j >= 0; j--) {

And I then get

16  12  8   4   
15  11  7   3   
14  10  6   2   
13  9   5   1   

Upvotes: 2

Related Questions