Create matrix whose sum of diagonals in each sub matrix is even | C ++ 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
#include <iostream> NEW_LINE using namespace std ; 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 ) { cout << even << " ▁ " ; even += 2 ; } else { cout << odd << " ▁ " ; odd += 2 ; } } cout << " STRNEWLINE " ; } } int main ( ) { int N = 4 ; evenSubMatrix ( N ) ; return 0 ; }