text: Construct a matrix with sum equal to the sum of diagonal elements | Java 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: public class Main { public static void constructmatrix ( int N ) { boolean check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { System . out . print ( "1 ▁ " ) ; } else if ( check ) { System . out . print ( "2 ▁ " ) ; check = false ; } else { System . out . print ( " - 2 ▁ " ) ; check = true ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 5 ; constructmatrix ( 5 ) ; } }