0% found this document useful (0 votes)
4 views2 pages

Python

sppu practical

Uploaded by

प्रथम
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python

sppu practical

Uploaded by

प्रथम
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

def generate_Magic_Square(size):

magicSquare=[[0 for x in range(size)] for y in range(size)]

i=size/2

j=size-1

num=1

while num<=(size*size):

if i==-1 and j==size:

j=size-2

i=0

else:

if j==size:

j=0

if i<0:

i=size-1

if magicSquare[int(i)][int(j)]:

j=j-2

i=i+1

continue

else:

magicSquare[int(i)][int(j)]=num

num=num+1

j=j+1

i=i-1

sum=size*(size*size+1)/2

print("Sum of each row or column is : ",sum)

print("Magic Square of size",size,"*",size,"is : \n")

for i in range(0,size):

for j in range(0,size):
print(' %2d ' % (magicSquare[i][j]),end=' | ')

if j==size-1:

print()

flag=1

while flag==1:

n=int(input("\nEnter the size of the MAGIC SQUARE : "))

if n%2==0:

s=int(input("Please enter an ODD Number (for example - 3,5,7,9,....) : "))

generate_Magic_Square(s)

else:

generate_Magic_Square(n)

a=input("\nDo you want to print Magic Square of some other size (yes/no) : ")

if a=='yes':

flag=1

else:

flag=0

print("\nThanks for using this program!")

You might also like