Find a Square Matrix such that sum of elements in every row and column is K | C ++ implementation of the approach ; Function to print the required matrix ; Print k for the left diagonal elements ; Print 0 for the rest ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) cout << k << " ▁ " ; else cout << "0 ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 3 , k = 7 ; printMatrix ( n , k ) ; return ( 0 ) ; }