Find a Square Matrix such that sum of elements in every row and column is K | Java implementation of the approach ; Function to print the required matrix ; Print k for the left diagonal elements ; Print 0 for the rest ; Driver code
import java . util . * ; class GFG { static void printMatrix ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) System . out . print ( k + " ▁ " ) ; else System . out . print ( "0 ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 3 , k = 7 ; printMatrix ( n , k ) ; } }