Advertisement
Radeen10-_

Test of a Magic Square Matrix

Feb 5th, 2021 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #declare a blank list
  2. a=[]
  3. for i in range(3):
  4.     b=[] #another blank list
  5.     for j in range(3):
  6.         j=int(input('enter number')) #for column
  7.         b.append(j) #put j value in b for making a list
  8.     a.append(b)
  9. print("matrix is ------")
  10.  
  11. for i in range(3):
  12.     for j in range(3):
  13.         print(a[i][j],end=" ")
  14.     print()
  15. #initialization of sum for primary and secondary diagonal sum
  16. sum1=0
  17. sum2=0
  18. for i in range(3):
  19.     for j in range(3):
  20.         #algorithm for primary and secondary diagonal sum
  21.         if i==j:
  22.             sum1=sum1+a[i][j]
  23. if sum1!=sum2:
  24.     boolean=0#denote boolean value as true or false
  25. else:
  26.     for i in range(3):
  27.         sum_row=0
  28.         sum_column=0
  29.         for j in range(3):
  30.             sum_row=sum_row+a[i][j]#summation of row
  31.             sum_column=sum_column+a[j][i]#summation of column
  32.             if sum_row!=sum1:
  33.                 boolean=0
  34.             elif sum_column!=sum1:
  35.                 boolean=0
  36.             else:
  37.                 boolean=1
  38. if boolean==1:
  39.       print("matrix is magic square")
  40. else:
  41.     print("matrix is not magic square")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement