# Check given matrix is magic square or not

Given a matrix, check whether it’s Magic Square or not. A Magic Square is a n x n matrix of distinct element from 1 to n2 where sum of any row, column or diagonal is always equal to same number.

Examples:

```Input : n = 3
2   7   6
9   5   1
4   3   8
Output : Magic matrix
Explanation:In matrix sum of each
row and each column and diagonals sum is
same = 15.

Input : n = 3
1   2   2
2   2   1
2   1   2
Output : Not a Magic Matrix
Explanation:In matrix sum of each
row and each column and diagonals sum is
not same.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. Find sum of prime diagonal and secondary diagonal.
2. Calculate sum of each rows and columns.
3. If the prime diagonal and secondary diagonal sums is equal to every row’s sum and every column’s sum, then it is magic matrix.

## C++

 `// C++ program to check whether a given  ` `// matrix is magic matrix or not ` `#include ` ` `  `#define N 3 ` `using` `namespace` `std; ` ` `  `// Returns true if mat[][] is magic ` `// square, else returns false. ` `bool` `isMagicSquare(``int` `mat[][N]) ` `{  ` `    ``// calculate the sum of  ` `    ``// the prime diagonal ` `    ``int` `sum = 0,sum2=0;  ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``sum = sum + mat[i][i]; ` `    ``// the secondary diagonal ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``sum2 = sum2 + mat[i][N-1-i]; ` ` `  `    ``if``(sum!=sum2)  ` `        ``return` `false``; ` ` `  `    ``// For sums of Rows  ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `         `  `        ``int` `rowSum = 0;      ` `        ``for` `(``int` `j = 0; j < N; j++) ` `            ``rowSum += mat[i][j]; ` `         `  `        ``// check if every row sum is ` `        ``// equal to prime diagonal sum ` `        ``if` `(rowSum != sum) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``// For sums of Columns ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `         `  `        ``int` `colSum = 0;      ` `        ``for` `(``int` `j = 0; j < N; j++) ` `            ``colSum += mat[j][i]; ` ` `  `        ``// check if every column sum is  ` `        ``// equal to prime diagonal sum ` `        ``if` `(sum != colSum)  ` `        ``return` `false``; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// driver program to  ` `// test above function ` `int` `main() ` `{ ` `    ``int` `mat[][N] = {{ 2, 7, 6 }, ` `                    ``{ 9, 5, 1 }, ` `                    ``{ 4, 3, 8 }}; ` `     `  `    ``if` `(isMagicSquare(mat)) ` `        ``cout << ``"Magic Square"``; ` `    ``else` `        ``cout << ``"Not a magic Square"``; ` `     `  `    ``return` `0; ` `} `

## Java

 `// JAVA program to check whether a given ` `// matrix is magic matrix or not ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `N = ``3``; ` `     `  `    ``// Returns true if mat[][] is magic ` `    ``// square, else returns false. ` `    ``static` `boolean` `isMagicSquare(``int` `mat[][]) ` `    ``{ ` `         `  `        ``// calculate the sum of ` `        ``// the prime diagonal ` `         ``int` `sum = ``0``,sum2=``0``;  ` `        ``for` `(``int` `i = ``0``; i < N; i++) ` `            ``sum = sum + mat[i][i]; ` ` `  `        ``// the secondary diagonal ` `        ``for` `(``int` `i = ``0``; i < N; i++) ` `            ``sum2 = sum2 + mat[i][N-``1``-i]; ` ` `  `        ``if``(sum!=sum2)  ` `            ``return` `false``; ` ` `  `        ``// For sums of Rows ` `        ``for` `(``int` `i = ``0``; i < N; i++) { ` ` `  `            ``int` `rowSum = ``0``; ` `            ``for` `(``int` `j = ``0``; j < N; j++) ` `                ``rowSum += mat[i][j]; ` ` `  `            ``// check if every row sum is ` `            ``// equal to prime diagonal sum ` `            ``if` `(rowSum != sum) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// For sums of Columns ` `        ``for` `(``int` `i = ``0``; i < N; i++) { ` ` `  `            ``int` `colSum = ``0``; ` `            ``for` `(``int` `j = ``0``; j < N; j++) ` `                ``colSum += mat[j][i]; ` ` `  `            ``// check if every column sum is ` `            ``// equal to prime diagonal sum ` `            ``if` `(sum != colSum) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// driver program to ` `    ``// test above function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `mat[][] = {{ ``2``, ``7``, ``6` `}, ` `                       ``{ ``9``, ``5``, ``1` `}, ` `                       ``{ ``4``, ``3``, ``8` `}}; ` ` `  `        ``if` `(isMagicSquare(mat)) ` `            ``System.out.println(``"Magic Square"``); ` `        ``else` `            ``System.out.println(``"Not a magic"` `+ ` `                                    ``" Square"``); ` `    ``} ` `} ` `// This code is contributed by vt_m `

## Python3

 `# Python3 program to check whether a given  ` `# matrix is magic matrix or not ` `N ``=` `3` ` `  `# Returns true if mat[][] is magic ` `# square, else returns false. ` `def` `isMagicSquare( mat) : ` `     `  `    ``# calculate the sum of  ` `    ``# the prime diagonal ` `    ``s ``=` `0`  `     `  `    ``for` `i ``in` `range``(``0``, N) : ` `        ``s ``=` `s ``+` `mat[i][i] ` ` `  `    ``# the secondary diagonal ` `    ``s2 ``=` `0` `    ``for` `i ``in` `range``(``0``, N) : ` `        ``s2 ``=` `s2 ``+` `mat[i][N``-``i``-``1``] ` ` `  `    ``if``(s!``=``s2) : ` `        ``return` `False` ` `  `    ``# For sums of Rows  ` `    ``for` `i ``in` `range``(``0``, N) : ` `        ``rowSum ``=` `0``;      ` `        ``for` `j ``in` `range``(``0``, N) : ` `            ``rowSum ``+``=` `mat[i][j] ` `         `  `        ``# check if every row sum is ` `        ``# equal to prime diagonal sum ` `        ``if` `(rowSum !``=` `s) : ` `            ``return` `False` ` `  `    ``# For sums of Columns ` `    ``for` `i ``in` `range``(``0``, N): ` `        ``colSum ``=` `0` `        ``for` `j ``in` `range``(``0``, N) : ` `            ``colSum ``+``=` `mat[j][i] ` ` `  `        ``# check if every column sum is  ` `        ``# equal to prime diagonal sum ` `        ``if` `(s !``=` `colSum) : ` `            ``return` `False` ` `  `    ``return` `True` ` `  `# Driver Code ` `mat ``=` `[ [ ``2``, ``7``, ``6` `], ` `        ``[ ``9``, ``5``, ``1` `], ` `        ``[ ``4``, ``3``, ``8` `] ] ` `     `  `if` `(isMagicSquare(mat)) : ` `    ``print``( ``"Magic Square"``) ` `else` `: ` `    ``print``( ``"Not a magic Square"``) ` `# This code is contributed by Nikita Tiwari.     `

## C#

 `// C# program to check whether a given ` `// matrix is magic matrix or not ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``static` `int` `N = 3; ` `     `  `    ``// Returns true if mat[][] is magic ` `    ``// square, else returns false. ` `    ``static` `bool` `isMagicSquare(``int``[,] mat) ` `    ``{ ` `         `  `        ``// calculate the sum of ` `        ``// the prime diagonal ` `        ``int` `sum = 0, sum2 = 0; ` ` `  `        ``for` `(``int` `i = 0; i < N; i++) ` `            ``sum = sum + mat[i, i]; ` ` `  `        ``// the secondary diagonal ` `        ``for` `(``int` `i = 0; i < N; i++) ` `            ``sum2 = sum2 + mat[i, N-1-i]; ` `      `  `        ``if``(sum!=sum2) ` `            ``return` `false``; ` ` `  `        ``// For sums of Rows ` `        ``for` `(``int` `i = 0; i < N; i++) { ` ` `  `            ``int` `rowSum = 0; ` `            ``for` `(``int` `j = 0; j < N; j++) ` `                ``rowSum += mat[i, j]; ` ` `  `            ``// check if every row sum is ` `            ``// equal to prime diagonal sum ` `            ``if` `(rowSum != sum) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// For sums of Columns ` `        ``for` `(``int` `i = 0; i < N; i++)  ` `        ``{ ` ` `  `            ``int` `colSum = 0; ` `            ``for` `(``int` `j = 0; j < N; j++) ` `                ``colSum += mat[j,i]; ` ` `  `            ``// check if every column sum is ` `            ``// equal to prime diagonal sum ` `            ``if` `(sum != colSum) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[,] mat =``new` `int` `[,] {{ 2, 7, 6 }, ` `                                 ``{ 9, 5, 1 }, ` `                                 ``{ 4, 3, 8 }}; ` ` `  `        ``if` `(isMagicSquare(mat)) ` `            ``Console.WriteLine(``"Magic Square"``); ` `        ``else` `            ``Console.WriteLine(``"Not a magic"` `+ ` `                              ``" Square"``); ` `    ``} ` `} ` `// This code is contributed by KRV. `

## PHP

 ` ` `// This code is contributed by nitin mittal `

```Magic square
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : KRV, nitin mittal, letMe_Do

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.