## Python Program:

- To print N x N magic matrix

#### Example:

User Input: 3

Output:

```8 1 6
3 5 7
4 9 2```

``````n=int(input("Enter your magic matrix size <Must be odd> : "))
if n%2==0 :
print("Magic matrix size must be an odd")
else :
m = [ * n for i in range(n)]
r=n;k=1
c=int(n/2)-1

for i in range(n):
for j in range(n):
r=(r+n)%n
c+=1
c%=n
m[r][c]=k
k+=1
if(k%n==1): r+=1; c-=1;
else: r-=1;

for i in range(n):
for j in range(n):
print(m[i][j], end="")

if(m[i][j]<10):print("    ", end="")
elif(m[i][j]<100):print("   ", end="")
elif(m[i][j]<1000):print("  ", end="")
else: print(" ", end="")
print("\n")
``````

### Output:

```  Enter your magic matrix size <Must be odd> : 5

17  24  1   8   15

23  5   7   14  16

4   6   13  20  22

10  12  19  21  3

11  18  25  2   9
```
Program
puzzle

Remember : Magic matrix are always odd value matrix

`like : 3x3,5x5,7x7,.....`

So, you must first check it for an Odd value

Main Logic :

``````''' n: size of matrix
m: Matrix used to save,
r: holds row position, c: holds column position,
k: value to put every time in matrix '''

m = [ * n for i in range(n)]
r=n;k=1
c=int(n/2)-1

for i in range(n):
for j in range(n):
r=(r+n)%n
c+=1
c%=n
m[r][c]=k
k+=1
if(k%n==1): r+=1; c-=1;
else: r-=1;``````

``````for i in range(n):
for j in range(n):
print(m[i][j], end="")

#To prettify Matrix size of upto 99
if(m[i][j]<10):print("    ", end="")
elif(m[i][j]<100):print("   ", end="")
elif(m[i][j]<1000):print("  ", end="")
else: print(" ", end="")
print("\n")``````

```How to write Magic Matrix

Always Put value in every step(Increment value by 1 every time)
================================================================

0. Start from
'top mid'

'right top'

2. If you found any number multiple of Matrix Size
'down'

3. continue from step 1

==============================================================

Example in 3x3 Magic Matrix
Step 0: Start from  'top mid'
_ 1 _
_ _ _
_ _ _

Step 1: Follow the direction 'right top'
_ 1 _
_ _ _
_ _ 2

Step 2: Follow the direction 'right top'(Not Multiple of 3, continue with rule 1)
_ 1 _
3 _ _
_ _ 2

Step 3: Follow the direction 'down'(Found Multiple of 3, rule 2)
_ 1 _
3 _ _
4 _ 2

Step 4: Follow the direction 'right top'(Not Multiple of 3, continue with rule 1)
_ 1 _
3 5 _
4 _ 2

Step 5: Follow the direction 'right top'(Not Multiple of 3, continue with rule 1)
_ 1 6
3 5 _
4 _ 2

Step 6: Follow the direction 'down'(Found Multiple of 3, rule 2)
_ 1 6
3 5 7
4 _ 2

Step 7: Follow the direction 'right top'(Not Multiple of 3, continue with rule 1)
8 1 6
3 5 7
4 _ 2

Step 7: Follow the direction 'right top'(Not Multiple of 3, continue with rule 1)
8 1 6
3 5 7
4 9 2
```
Concept

Coming Soon !

Quick
Tutorial

# Quote

An essential aspect of creativity is not being afraid to fail.