text: Construct a matrix with sum equal to the sum of diagonal elements | C # program to implement the above approach ; Function to construct matrix with diagonal sum equal to matrix sum ; If diagonal position ; Positive element ; Negative element ; Driver Code
code: using System ; class GFG { public static void constructmatrix ( int N ) { bool check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { Console . Write ( "1 ▁ " ) ; } else if ( check ) { Console . Write ( "2 ▁ " ) ; check = false ; } else { Console . Write ( " - 2 ▁ " ) ; check = true ; } } Console . WriteLine ( ) ; } } static public void Main ( ) { int N = 5 ; constructmatrix ( N ) ; } }