
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Check given matrix is magic square or not in C++
- In a magic square each row, column, and diagonal have the same sum. Check which of the following is a magic square.
- Swift program to check if a given square matrix is an Identity Matrix
- Check if a Matrix is Identity Matrix or not in Java?
- Check if a Matrix is Markov Matrix or not in Java
- Check if a Matrix is Involutory or not in Java?
- Magic Square
- In a “magic square”, the sum of the numbers in each row, in each column and along the diagonals is the same. Is this a magic square?
- How to check if a matrix in R is in binary form?
- Check if matrix can be converted to another matrix by transposing square sub-matrices in Python
- Check if a number is magic (Recursive sum of digits is 1) in C++
- How to check if a matrix is invertible or not in R?
- Program to check if a matrix is Binary matrix or not in C++
- Program to check if a matrix is symmetric in C++
- Check if a Matrix is Invertible in C++
