# Magic square java program – Java Program to Check Whether the Matrix is a Magic Square or Not

Magic square java program: In the previous article, we have discussed Java Program to Check Whether the Matrix is a Diagonal Matrix or Not

In this article we are going to see how we can check if a matrix is a magic square or not in JAVA language.

## Java Program to Check Whether the Matrix is a Magic Square or Not

Magic square program java: A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Magic square is a matrix of size NxN, containing elements from 1 to N in such arrangements that the sum of rows, columns, diagonals all individually add upto the same number.

Let’s see different ways to check whether a matrix is magic square or not.

### Method-1: Java Program to Check Whether the Matrix is a Magic Square or Not By Static Initialization of Array Elements

Approach:

• Initialize and declare one array of size 3×3 with elements.
• Check if the sum of row, col and all diagonals are same.
• If all sum are same then it is said to be a magic square else not.

Program:

public class Matrix
{
//Driver Method
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{1,1,1},{1,1,1},{1,1,1}}, temp[][] = new int[3][3];
int row, col;

System.out.print("The matrix elements are:");
printMatrix(arr);

if(checkMagicSquare(arr))
System.out.println("\nIt is a magic square");
else
System.out.println("\nIt is not a magic square");

}

// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}

//checkMagicSquare() method to check magic square
static boolean checkMagicSquare(int arr[][])
{
// Checks whether the diagonals sum are same or not
int sum1 = 0,sum2=0;
for (int i = 0; i < 3; i++)
{
sum1 += arr[i][i];
sum2 += arr[i][2-i];
}

if(sum1!=sum2)
return false;

// Checks wether the row sum is equal to the column sum
for (int i = 0; i < 3; i++)
{
int rowSum = 0, colSum = 0;
for (int j = 0; j < 3; j++)
{
rowSum += arr[i][j];
colSum += arr[j][i];
}
if (rowSum != colSum || colSum != sum1)
return false;
}

return true;
}
}


Output:

The matrix elements are:
1 1 1
1 1 1
1 1 1

It is a magic square

### Method-2: Java Program to Check Whether the Matrix is a Magic Square or Not By Dynamic Initialization of Array Elements

Approach:

• Declare one array of size 3×3.
• Use two for loops to input elements.
• Check if the sum of row, col and all diagonals are same.
• If all sum are same then it is said to be a magic square else not.

Program:

import java.util.Scanner;
public class matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col;

// Taking matrix input
System.out.println("Enter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();

System.out.print("The matrix elements are:");
printMatrix(arr);

if(checkMagicSquare(arr))
System.out.println("\nIt is a magic square");
else
System.out.println("\nIt is not a magic square");

}

// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}

//checkMagicSquare() method to check magic square
static boolean checkMagicSquare(int arr[][])
{
// Checks whether the diagonals sum are same or not
int sum1 = 0,sum2=0;
for (int i = 0; i < 3; i++)
{
sum1 += arr[i][i];
sum2 += arr[i][2-i];
}

if(sum1!=sum2)
return false;

// Checks wether the row sum is equal to the column sum
for (int i = 0; i < 3; i++) {

int rowSum = 0, colSum = 0;
for (int j = 0; j < 3; j++)
{
rowSum += arr[i][j];
colSum += arr[j][i];
}
if (rowSum != colSum || colSum != sum1)
return false;
}
return true;
}

}

Output:

Enter matrix elements : 1 1 1 1 1 1 1 1 1
The matrix elements are:
1 1 1
1 1 1
1 1 1

It is a magic square

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Test Yourself:

1. Magic square 3×3 java?
2. Magic square python code?
3. Magic square matrix example?
4. Magic square java user input?
5. Magic square program in java?
6. Java program to check whether the matrix is a magic square or not?
7. Write a program to check if a square matrix is a x matrix or not?
8. Write a program to find whether a given matrix is a magic square or not?
9. C program to check whether matrix is magic square or not?

Related Java Programs: