SE Computer Engineering practical 6 solution

SE Computer Engineering practical 6 solution

Question:

Write a python Program for magic square.
A magic square is an n * n matrix of the integers 1 to n2 such that the sum of each row, column, and diagonal is the same.
The figure given below is an example of magic square for case n=5.
In this example, the common sum is 65.

Code:

def generateSquare(n):
   # 2-D array with all
   # slots set to 0
   magicSquare = [[0 for x in range(n)]
                  for y in range(n)]
   # initialize position of 1
   i = n / 2
   j = n - 1
   # Fill the square by placing values
   num = 1
   while num <= (n * n):
       if i == -1 and j == n:  # 3rd condition
           j = n - 2
           i = 0
       else:
           # next number goes out of
           # right side of square
           if j == n:
               j = 0
               # next number goes
           # out of upper side
           if i < 0:
               i = n - 1
       if magicSquare[int(i)][int(j)]:  # 2nd condition
           j = j - 2
           i = i + 1
           continue
       else:
           magicSquare[int(i)][int(j)] = num
           num = num + 1
       j = j + 1
       i = i - 1  # 1st condition
   # Printing the square
   print("Magic Square for n =", n)
   print("Sum of each row or column", n * (n * n + 1) / 2, "\n")
   for i in range(0, n):
       for j in range(0, n):
           print('%2d ' % (magicSquare[i][j]), end='')
           # To display output
           # in matrix form
           if j == n - 1:
               print()


# Driver Code
# Works only when n is odd
n = int(input("Number of rows of the Magic Square:"))
generateSquare(n)

Output:

Number of rows of the Magic Square:5
Magic Square for n = 5
Sum of each row or column 65.0 

 9  3 22 16 15 
 2 21 20 14  8 
25 19 13  7  1 
18 12  6  5 24 
11 10  4 23 17 

Process finished with exit code 0
Tech Amplifier Final Logo