Question
Write a Java code A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an n x n matrix, with
Write a Java code
A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an n x n matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. Here is an example of a magic square:
2 7 6
9 5 1
4 3 8
If we sum up the numbers on each row, (2+7+6, 9+5+1, and 4+3+8) the results are the same, which is 15. The same goes for the sum of each column, (2+9+4, 7+5+3, and 6+1+8), and each diagonal (2+5+8 and 6+5+4). Not every square grid of numbers is a magic square, only when it satisfies this rule. A magic square can be composed of 9 numbers (3 by 3 grid), 16 numbers (4 by 4), etc.
Write a java program that takes a series of integer variables as an input and arranges them in a 2-dimensional (2-D) array. Your program should determine whether the entered series of numbers form a Magic Square or not. Below is a sample output of the program:
Enter numbers --> 1 2 3 4 5 6 7 8 9
The square is:
1 2 3
4 5 6
7 8 9
This is NOT a magic square.
Enter numbers --> 8 1 6 3 5 7 4 9 2
The square is:
8 1 6
3 5 7
4 9 2
This is a magic square!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started