8 1 6 3 5 7 4 9 2
Is a magic square. A magic square of order n is an n by n square contains the numbers 1 to n squared such that the sum of each row, each column and the two main diagonals is constant and equal to (n + n3) / 2
If n is odd there is a simple algorithm for constructing such a square.
Write a program that will accept an odd integer between 1 .. 15 and generate the magic square.
Sample output
Please enter the size of the magic square (0 to quit):2 Not an odd value Please enter the size of the magic square (0 to quit):111 Two large Please enter the size of the magic square (0 to quit):9 47 58 69 80 01 12 23 34 45 57 68 79 09 11 22 33 44 46 67 78 08 10 21 32 43 54 56 77 07 18 20 31 42 53 55 66 06 17 19 30 41 52 63 65 76 16 27 29 40 51 62 64 75 05 26 28 39 50 61 72 74 04 15 36 38 49 60 71 73 03 14 25 37 48 59 70 81 02 13 24 35 Please enter the size of the magic square (0 to quit):
| © Allan Kochis | Last revision 11/10/2005 |