A magic square is a 4x4 square of numbers, where the sums of each row, the sums of each column, and the sums of each diagonal, are equal to each other. A super magic square is a magic square that uses every number from 1-16 exactly once. Write a program that takes in 16 numbers between 1 and 16, and outputs whether it is a magic square, super magic square, or a non-magic square.
You can read more about magic squares here: https://en.wikipedia.org/wiki/Magic_square (the terminology is a bit different).
Sample inputs:
1).
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
2).
2 3 5 8
5 8 2 3
4 1 7 6
7 6 4 1
3).
1 1 1 1
5 7 1 4
9 11 1 5
6 6 6 1
Sample outputs:
1). Super magic square!
2). Magic square!
3). Not magic
Lucas-import java.util.*; class Main { public static void main(String[] args) { int i, j; int sum_row, sum_col, sum_diagonal = 0, sum = 0; boolean thing=true; int[][] magic square = new int[4][4]; Scanner in = new Scanner(System.in); System.out.println("Please enter the numbers in your square with spaces and in the shape of a 4x4 square(with all numbers once from 1-16)."); for (i=0; i<4; i++) for (j=0; j<4; j++) magic square[i][j] = in.nextInt(); for (i=0; i<4; i++) { for (j=0; j<4; j++) System.out.print(magic square[i][j] + " "); System.out.println(); } for (j=0; j<4; j++) sum += magic square[0][j]; for (i=1; i<4; i++) { sum_row = 0; for (j=0; j<4; j++) sum_row += magic square[i][j]; if (sum_row != sum) { thing = false; break; } } if (thing) { for (j=0; j<4; j++) { sum_col = 0; for (i=0; i<4; i++) sum_col += magic square[i][j]; if (sum_col != sum) { thing = false; break; } } } if (thing) { for (i=0; i<4; i++) for (j=0; j<4; j++) if (i==j) sum_diagonal += magic square[i][j]; if (sum_diagonal != sum) { thing = false; } } if (thing) { sum_diagonal = 0; for (i=0; i<4; i++) for (j=0; j<4; j++) if ((i+j) == 2) sum_diagonal += magic square[i][j]; if (sum_diagonal != sum) { thing = false; } } if (thing) System.out.println("It's a magic square."); else System.out.println("It's a non-magic square."); } }