# How to Check if the Matrix is a Magic Square in Java?

Matrices are nothing but a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.

As per the problem statement the task is to check if the matrix is a magic square or not.

A matrix is said to be a magic square if the sum of elements of any row, column or diagonal is equal to a specific number.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

## To show you some instances

### Instance-1

Suppose the original matrix is {
{ 13, 8, 15 },
{ 14, 12, 10 },
{ 9, 16, 11 }
}; 

Here the sum of any row or any column or diagonal is equal to 36.

After checking for magic matrix, the result index will be

Given matrix is a Magic Square

### Instance-2

Suppose the original matrix is{
{ 8, 7, 6 },
{ 9, 5, 1 },
{ 5, 3, 8 }
}; 

Here the sum of any row or any column or diagonals are not equal.

After checking for magic matrix, the result index will be

Given matrix is a not a magic Square

### Algorithm

Step 1 − Initialise and declare the matrix

Step 2 − Declare Boolean to check for magic square.

Step 3 − Find the sum of the two diagonals using a loop.

Step 4 − Find the sum of the rows and columns using a for loop.

Step 5 − Check for magic square.

Step 6 − Print the result.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it-

array.length


where, ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Matrix

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Matrix

In this approach, matrix elements will be initialized in the program. Then as per the algorithm check if the matrix is a magic square or not.

### Example

public class Main {
//main method
public static void main(String[] args){

//Initialising and declaring matrix
int mat[][] = {{ 13, 8, 15 }, { 14, 12, 10 }, { 9, 16, 11 }};
int M = 3;

//declare boolean to check for magic square or not
boolean flag = false;

//Initialising and declaring the diagonal sum as 0
int sum1 = 0,sum2=0;

//finding the sum of the two diagonals i.e. sum1 and sum2
for (int i = 0; i < M; i++){
sum1 += mat[i][i];
sum2 += mat[i][M-1-i];
}

//check if sum of diagonals are unequal then it is not a magic square
if(sum1!=sum2)
flag = true;
for (int i = 0; i < M; i++) {

//Initialising and declaring the row sum and column sum as 0
int rowSum = 0, colSum = 0;

//finding the sum of the rows and columns i.e. row and column
for (int j = 0; j < M; j++){
rowSum += mat[i][j];
colSum += mat[j][i];
}

//check if sum of rows, columns and diagonals are unequal then it is not a magic square
if (rowSum != colSum || colSum != sum1)
flag = true;
}

//checking and printing magic square
if (!flag)
System.out.println("Given matrix is a Magic Square");
else
System.out.println("Given matrix is a not a magic" + " Square");
}
}


### Output

Given matrix is a Magic Square


## Approach-2: By Using User Defined Method

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside the method as per the algorithm check if the matrix is a magic square or not.

### Example

public class Main {
//main method
public static void main(String[] args){

//Initialising and declaring matrix
int mat[][] = {{ 8, 7, 6 }, { 9, 5, 1 }, { 5, 3, 8 }};
//calling user defined function
magicSquare(mat);
}
static int M = 3;

//user defined method
static void magicSquare(int mat[][]) {

//declare boolean to check for magic square or not
boolean flag = false;

//Initialising and declaring the diagonal sum as 0
int sum1 = 0,sum2=0;

//finding the sum of the two diagonals i.e. sum1 and sum2
for (int i = 0; i < M; i++)	{
sum1 += mat[i][i];
sum2 += mat[i][M-1-i];
}

//check if sum of diagonals are unequal then it is not a magic square
if(sum1!=sum2)
flag = true;
for (int i = 0; i < M; i++) {

//Initialising and declaring the rows and columns sum as 0
int rowSum = 0, colSum = 0;

//finding the sum of the rows and columns i.e. row and column
for (int j = 0; j < M; j++)	{
rowSum += mat[i][j];
colSum += mat[j][i];
}

//check if sum of rows, columns and diagonals are unequal then it is not a magic square
if (rowSum != colSum || colSum != sum1)
flag = true;
}

//checking and printing magic square
if (!flag)
System.out.println("Given matrix is a Magic Square");
else
System.out.println("Given matrix is a not a magic" + " Square");
}
}


### Output

Given matrix is a not a magic Square


In this article, we explored different approaches to check if the matrix is a magic square or not by using Java programming language.