# How to check the given matrix is magic square or not in C++

Hello,

In this tutorial, we will learn how to check, whether a given matrix is a magic square matrix or not, with the algorithm and a C++ program.

A magic square matrix is a square matrix in which the sum of each row, column, and diagonals are the same.

The conditions for the magic square is quite simple:

- The length of the row and column should be the same for a square matrix.
- The sum of each row, column and diagonal should be the same.

## Magic Square Matrix

let’s take an example to check, whether the square matrix is magic square or not.

**Example: **

input matrix: 8 1 6 3 5 7 4 9 2 Here, we can see that thecondition 1is satisfied as the input matrix has dimention 3 x 3.Now, finding the sum of each row:sum of first row: 8 + 1 + 6 = 15 sum of second row: 3 + 5 + 7 = 15 sum of third row: 4 + 9 + 2 = 15Now, finding the sum of each column:sum of first column: 8 + 3 + 4 = 15 sum of second column: 3 + 5 + 7 = 15 sum of third column: 4 + 9 + 2 = 15Now, finding the sum of each diagonal:sum of first diagonal: 8 + 5 + 2 = 15 sum of second diagonal: 6 + 5 + 4 = 15 since all the sum of each row, column and diagonal is same, therefore,condition 2is also satisfied. Now, we can see that both the conditions are satisfied. Therefore, the matrix is a magic square matrix.

**Algorithm to check a matrix is magic square or not**

- Declare and initialize a matrix.
- check for the equality of the length of the row and column of the matrix.
- Find the sum of first and second diagonal elements. return 0 if bot the diagonal sum is not equal, else continue next steps.
- Find the sum of each row and for each row sum, check for the equality with the sum of one of the diagonal. If the sum of any row is not equal to the sum of diagonal then return 0. else, continue for the next steps.
- Find the sum of each column and for each column sum, check for the equality with the sum of one of the diagonal. If the sum of any column is not equal to the sum of diagonal then return 0. else, continue for the next steps.
- if all the above steps are valid, then return 1.

### C++ program to check the given matrix is magic square or not

#include <bits/stdc++.h> using namespace std; int isMagicSquare(int matrix[50][50], int n, int m); int main(){ int matrix[50][50], n, m; cout<<"Enter the length of row of the matrix: "; cin >> m; cout<<"Enter the length of column of the matrix: "; cin >> n; if(n != m){ cout<<"Length of roe and column should be equal."; exit(0); } cout << "\nInput the element of matrix: "; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin>>matrix[i][j]; if (isMagicSquare(matrix, n, m)) cout << "The matrix is Magic Square"; else cout << "The matrix is Not a magic Square"; return 0; } int isMagicSquare(int matrix[50][50], int n, int m){ int sum1 = 0, sum2=0; //finding the sum of first diagonal for (int i = 0; i < n; i++) sum1 = sum1 + matrix[i][i]; //finding the sum of seciond diagonal for (int i = 0; i < n; i++) sum2 = sum2 + matrix[i][n - 1 - i]; //Checking, whether the sum of two diagonal is same or not if(sum1 != sum2) return 0; //finding the sum of each row for (int i = 0; i < n; i++){ int rowSum = 0; for (int j = 0; j < m; j++) rowSum = rowSum + matrix[i][j]; //checking, whether eachh row sum is equal to diagonal sum or not if (rowSum != sum1) return 0; } //finding the sum of each column for (int i = 0; i < n; i++){ int columnSum = 0; for (int j = 0; j < n; j++) columnSum += matrix[j][i]; //checking, whether each column sum is equal to diagonal sum or not if (columnSum != sum1) return 1; } return true; }

**Output: **

Enter the length of row of the matrix: 3 Enter the length of column of the matrix: 3 Input the element of matrix: 8 1 6 3 5 7 4 9 2 The matrix is Magic Square

**Time complexity: **O(n^2), where n is the length od row and column.

In this way, you can check if a matrix is a magic square matrix or not in C++.

**You may also read: **

Need a code for a game where players allowed to enter digits in a matrix until it is square. Score will be decided by T+P*100 where T is the total time taken for entering digits and P is the number of alteration

The matrix I mention in my question should be magic square matrix