A magic square is a grid of numbers in which the sum of the numbers in each row, column, and diagonal is the same. It is called a "magic" square because the sum is the same no matter which direction you add up the numbers.
To create a magic square in Java, we can use a two-dimensional array to represent the grid of numbers. We can then use a loop to initialize the values in the array and to check if the square is magic.
Here is an example of how we could create a 3x3 magic square in Java:
int[][] magicSquare = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
magicSquare[i][j] = (i * 3) + (j + 1);
}
}
boolean isMagic = true;
int magicSum = magicSquare[0][0] + magicSquare[1][1] + magicSquare[2][2];
for (int i = 0; i < 3; i++) {
int rowSum = 0;
int colSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += magicSquare[i][j];
colSum += magicSquare[j][i];
}
if (rowSum != magicSum || colSum != magicSum) {
isMagic = false;
break;
}
}
if (isMagic) {
System.out.println("This is a magic square!");
} else {
System.out.println("This is not a magic square.");
}
In this code, we first initialize the values in the array using a nested loop. The values are set to the position of each element in the array, starting with 1 in the top left corner and increasing by one for each element to the right and down.
Next, we check if the square is magic by comparing the sum of the numbers in each row, column, and diagonal to the sum of the numbers in the first row. If any of these sums are different, we set the isMagic
variable to false and break out of the loop.
Finally, we print a message indicating whether or not the square is magic.
This is just one way to create and check for a magic square in Java. There are many other ways to do it, and you can even create magic squares of different sizes by modifying the size of the array and the loop conditions.
java code for magic square Code Example
. If you have tried all values, return false. Similarly, the sum of each diagonal is also 15 hence the given matrix is a magic square. Similarly, if the calculated position of the column becomes m, then we will wrap it around to 0. A magic square contains the integers from 1 to n 2. Do you want to share more information about the topic discussed above or do you find anything incorrect? I'm sure you can keep improving this, and at the moment, it doesn't stop after finding the first solution.
Magic Square
By Michael Hartley On another page there's a java applet that Ten years ago, a mathematician called Yasusi Kanada wrote the original java code. Moreover you have to reset sum after each summation of row or column is been done. If you like GeeksforGeeks and would like to contribute, you can also write an article using write. While in happy number, we recursively calculate the sum of the square of digits until we get a single digit 1. Here is my improved version of my Magic square program from Any help for improvements would be really appreciated. Space Complexity: The space complexity of the above program is O n 2 , where n is the total number of rows or columns present in the square matrix. In order for a non-abstract class to be useful, usually it will need to have at least one method other than the constructor.
Magic square java program
Perhaps you are simply running the code in a single file but in larger projects you will likely need to use multiple files. The values in each row, column, and diagonal must add to 15. On the next row, it repeats this with 12 values for the fifth cell, 11 for the next, 10 for the next, and computes the value for the eighth cell. The sum of each row and column is 15. Rule 1: At any position, the position of the next number is computed by decreasing the row number of the previously filled number by 1 and increasing the column number of the previously filled number by 1. A magic square is one where the sum of each row, column, and diagonal is the same. 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.
Magic Square Java Code
For example, 325 is a magic number because the sum of its digits 3+2+5 is 10, and again sum up the resultant 1+0 , we get a single digit 1 as the result. Apparently something is wrong with this code and am not sure what. See your article appearing on the GeeksforGeeks main page and help other Geeks. A magic square of the order n has the numbers from 1 to m 2 1 and m 2 inclusive in such a way that the sum of all the numbers present in a row is equal to the sum of all the numbers present in a column, which in turn is equal to the sum of all the numbers present in a diagonal. At any time, if the calculated row position becomes -1, it will wrap around to n-1.
Java Magic Square 2d Array
If not, you need to blank your square and try the next value. Hence, the number 325 is a magic number. For example, if I put in the number 3 as the odd number, my output is: 6 1 0 3 4 5 9 7 8 0 isn't supposed to be there but the number two is. So, just to clarify, the box is getting populated in a diagonal pattern, which as a result, makes it impossible to have the same number in the same row or column appear more than once? We can further improve this. This new algorithm tries 16 different values for the first cell, 15 for the second, 14 for the third, and computes the value for the fourth cell.
for loop
Then continue exploration of the rows and columns, alternating between rows and columns as you go. Here, we already have 1 known value from row 0 , so only need to explore 3 unknowns by picking 2 and computing the 3rd. If not possible, follow these steps. If true, return true, if false, try with another value. You're actually writing the value 2 to the second row, third column. At any point of time, if the calculated position of the row becomes -1, then we will wrap it around to m - 1.