# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Java Magic Square 2d Array | A magic square is a matrix that has a different integer, the sum of a number in every row, column, and diagonally is the same. This sum value is called a magic constant.
The formula to find the magic constant is M = n[(n^2+1) / 2]
Where, n = is the order of the matrix.
Observe the below examples of Java magic square 2d array:-
2 7 6
9 5 1
4 3 8
The above matrix is of 3X3 hence applying the above formula, magic constant M => 3[(3^2+1) / 2] = 15. The sum of each row and column is 15. Similarly, the sum of each diagonal is also 15 hence the given matrix is a magic square.
Let us see another example in a different order.
9 3 22 16 15
2 21 20 14 8
25 19 13 7 1
18 12 6 5 24
11 10 4 23 17
This is a 5X5 matrix hence the magic constant will be 5(52+1)/2 = 65.
Java Magic Square 2d Array Code
Now let us see a Java program to generate a magic square of the given order.
public class Main {
public static void magicSquare(int n) {
int[][] magic = new int[n][n];
int i = n / 2;
int j = n - 1;
for (int num = 1; num <= n * n;) {
if (i == -1 && j == n) {
j = n - 2;
i = 0;
} else {
if (j == n) {
j = 0;
}
if (i < 0) {
i = n - 1;
}
}
if (magic[i][j] != 0) {
j -= 2;
i++;
continue;
} else {
magic[i][j] = num++;
}
j++;
i--;
}
System.out.println("The Magic Square for order " + n + ",");
System.out.println("Magic constant: " + n * (n * n + 1) / 2);
System.out.println("Matrix: ");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(magic[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int order = 3;
magicSquare(order);
}
}
Output:-
The Magic Square for order 3,
Magic constant: 15
Matrix:
2 7 6
9 5 1
4 3 8
Test-case:- Demonstrating Java magic square 2d array code of order 5×5
public class Main {
public static void magicSquare(int n) {
int[][] magic = new int[n][n];
int i = n / 2;
int j = n - 1;
for (int num = 1; num <= n * n;) {
if (i == -1 && j == n) {
j = n - 2;
i = 0;
} else {
if (j == n) {
j = 0;
}
if (i < 0) {
i = n - 1;
}
}
if (magic[i][j] != 0) {
j -= 2;
i++;
continue;
} else {
magic[i][j] = num++;
}
j++;
i--;
}
System.out.println("The Magic Square for order " + n + ",");
System.out.println("Magic constant: " + n * (n * n + 1) / 2);
System.out.println("Matrix: ");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(magic[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int order = 3;
magicSquare(order);
}
}
Output:-
The Magic Square for order 5,
Magic constant: 65
Matrix:
9 3 22 16 15
2 21 20 14 8
25 19 13 7 1
18 12 6 5 24
11 10 4 23 17
Also see:- Java Count Occurrences in 2D Array
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!