Permutations in Java: Rearranging Objects in an Array in Every Possible Way

Programming Assignment: Building Magic Squares

Print out every possible 3 x 3 magic square. In a magic square, all rows, columns and diagonals have the same sum (which is 15).


  • Store your square as a 1-dimensional array of integers 1-9, not a 2-dimensional array. This will make it easier to calculate the permutations.
  • Use what you have learned about permutations to try every possibile of rearrangent. Check to see for which cases the result is a magic square and print out only those cases
  • When printing your 1-dimensional array, print only three entries on each line so the array prints like a square.
  • You should get 8 different results