Create matrix whose sum of diagonals in each sub matrix is even | 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 ; Given order of matrix ; Function call
function evenSubMatrix ( N ) { let even = 1 ; let odd = 2 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { document . write ( even + " " ) ; even += 2 ; } else { document . write ( odd + " " ) ; odd += 2 ; } } document . write ( " " ) ; } } let N = 4 ; evenSubMatrix ( N ) ;