Given two integers **N** and **K**, the task is to find an **N** x **N** square matrix such that sum of every row and column should be equal to **K**. **Note** that there can be multiple such matrices possible. Print any one of them.

**Examples:**

Input:N = 3, K = 15

Output:

2 7 6

9 5 1

4 3 8

Input:N = 3, K = 7

Output:

7 0 0

0 7 0

0 0 7

**Approach:** An **N x N** matrix such that each left diagonal element is equal to **K** and rest elements are **0** will satisfy the given condition. In this way, the sum of the elements of the each row and column will be equal to **K**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the ` `// required matrix ` `void` `printMatrix(` `int` `n, ` `int` `k) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` ` ` `// Print k for the left ` ` ` `// diagonal elements ` ` ` `if` `(i == j) ` ` ` `cout << k << ` `" "` `; ` ` ` ` ` `// Print 0 for the rest ` ` ` `else` ` ` `cout << ` `"0 "` `; ` ` ` `} ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3, k = 7; ` ` ` ` ` `printMatrix(n, k); ` ` ` ` ` `return` `(0); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print the required matrix ` `static` `void` `printMatrix(` `int` `n, ` `int` `k) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` `{ ` ` ` ` ` `// Print k for the left ` ` ` `// diagonal elements ` ` ` `if` `(i == j) ` ` ` `System.out.print(k + ` `" "` `); ` ` ` ` ` `// Print 0 for the rest ` ` ` `else` ` ` `System.out.print(` `"0 "` `); ` ` ` `} ` ` ` `System.out.print(` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `, k = ` `7` `; ` ` ` ` ` `printMatrix(n, k); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to print the ` `# required matrix ` `def` `printMatrix(n, k) : ` ` ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `for` `j ` `in` `range` `(n) : ` ` ` ` ` `# Print k for the left ` ` ` `# diagonal elements ` ` ` `if` `(i ` `=` `=` `j) : ` ` ` `print` `(k, end ` `=` `" "` `); ` ` ` ` ` `# Print 0 for the rest ` ` ` `else` `: ` ` ` `print` `(` `"0"` `, end ` `=` `" "` `); ` ` ` ` ` `print` `(); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` `; k ` `=` `7` `; ` ` ` ` ` `printMatrix(n, k); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print the required matrix ` `static` `void` `printMatrix(` `int` `n, ` `int` `k) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `{ ` ` ` ` ` `// Print k for the left ` ` ` `// diagonal elements ` ` ` `if` `(i == j) ` ` ` `Console.Write(k + ` `" "` `); ` ` ` ` ` `// Print 0 for the rest ` ` ` `else` ` ` `Console.Write(` `"0 "` `); ` ` ` `} ` ` ` `Console.Write(` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3, k = 7; ` ` ` ` ` `printMatrix(n, k); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

7 0 0 0 7 0 0 0 7

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Product of middle row and column in an odd square matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Replace every matrix element with maximum of GCD of row or column
- Sum of matrix element where each elements is integer division of row and column
- Find all matrix elements which are minimum in their row and maximum in their column
- Find an N x N grid whose xor of every row and column is equal
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Print an N x M matrix such that each row and column has all the vowels in it
- Program to find the Sum of each Row and each Column of a Matrix
- Place N^2 numbers in matrix such that every row has an equal sum
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Count number of islands where every island is row-wise and column-wise separated
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of middle row and column in Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.