Menu
Question Index
...

What will be the output of the following program?

public class Event {
    public static void main(String[] args) {
        int order = 3;
        for (int row = 0; row < order; row++) {
            for (int col = 0; col < order; col++) {
                int rowMatrix = (((order + 1) / 2 + row + col) % order);
                int colMatrix = (((order + 1) / 2 + row + order - col - 1) % order) + 1;
                System.out.print(((rowMatrix * order) + colMatrix) + "\t");
            }
            System.out.println();
        }
    }
}


1    2    3    
4    5    6    
7    8    9    
8    1    6    
3    5    7    
4    9    2    
8    1    6    
3    5    2    
4    9    7    
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Nested for Loop In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App