Create matrix whose sum of diagonals in each sub matrix is even | Java program for the above approach ; Function to print N * N order matrix with all sub - matrix of even order is sum of its diagonal also even ; Even index ; Odd index ; Iterate two nested loop ; For even index the element should be consecutive odd ; For odd index the element should be consecutive even ; Driver code ; Given order of matrix ; Function call
import java . util . * ; class GFG { static void evenSubMatrix ( int N ) { int even = 1 ; int odd = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { System . out . print ( even + " ▁ " ) ; even += 2 ; } else { System . out . print ( odd + " ▁ " ) ; odd += 2 ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 4 ; evenSubMatrix ( N ) ; } }