# How to generate an odd order magic square

Magic Squares are the squares such that sum of elements in each row, sum of elements in each column are same. In this article, I am going to discuss how to generate a 'n' order magic square with elements from '1' to 'n*n' where 'n' is odd.

## Magic Squares

Magic Squares are the squares such that sum of elements in each row, sum of elements in each column are same. If the magic square is of odd order, sum of elements in each diagonal is also included. Suppose I rotate a magic square, another magic square will be formed. Hence, note that many magic squares can exist with same order. The following image shows a order 4 magic square and a order 5 magic square.

### Method for generation of an odd order magic square

**Procedure**

To generate a n (odd) order magic square with elements from 1 to n

^{2}, let us consider two data structures CURRENT_CELL and NUMBER to store the cell which is just filled and number which is just placed in the CURRENT_CELL respectively. Place the number (NUMBER) 1 in the middle cell of the last column (CURRENT_CELL). Follow these rules until the square is completely filled.

1) If CURRENT_CELL is an interior cell or border cell (top/left), place the next number (NUMBER++) in the cell south east of the CURRENT_CELL. Now, the cell just filled becomes CURRENT_CELL. In case the cell south east to the CURRENT_CELL is already filled, place the next number (NUMBER++) in the cell preceding CURRENT_CELL. Now, the cell just filled becomes CURRENT_CELL.

2) If CURRENT_CELL is a border (bottom/right) cell, place the next number (NUMBER++) in the first cell of the next row/column. Now, the cell just filled becomes CURRENT_CELL. In case CURRENT_CELL is the cell at bottom right corner of the square, place the next number in the cell preceding CURRENT_CELL. Now, the cell just filled becomes CURRENT_CELL.

Here, the border cells are nothing but the cells which belong to first, last rows or first, last columns. The cells which are not border cells are referred to as interior cells.

**Example**

To understand this method clearly, let us generate a order 3 magic square with elements from 1 to 9.

1 : Place 1 in the middle cell of the last column.

2 : CURRENT_CELL is a border (bottom/right) cell. Place 2 in first cell of next row (Rule 2).

3 : CURRENT_CELL is a border (bottom/right) cell. Place 3 in first cell of next column (Rule 2).

4 : Cell south east to CURRENT_CELL is filled. Place 4 in the cell preceding CURRENT_CELL (Rule 1).

5 : CURRENT_CELL is a border (top/left) cell. Place 5 in the cell south east to CURRENT_CELL (Rule 1).

6 : CURRENT_CELL is an interior cell. Place 6 in the cell south east to CURRENT_CELL (Rule 1).

7 : CURRENT_CELL is the bottom right corner. Place 7 in the cell preceding the CURRENT_CELL (Rule 2).

8 : CURRENT_CELL is a border (bottom/right) cell. Place 8 in first cell of next column (Rule 2).

9 : CURRENT_CELL is a border (bottom/right) cell. Place 9 in first cell of next row (Rule 2).

Sum of elements in Row 1 : 4 + 3 + 8 = 15

Sum of elements in Row 2 : 9 + 5 + 1 = 15

Sum of elements in Row 3 : 2 + 7 + 6 = 15

Sum of elements in Column 1 : 4 + 9 + 2 = 15

Sum of elements in Column 2 : 3 + 5 + 7 = 15

Sum of elements in Column 3 : 8 + 1 + 6 = 15

Sum of elements in Diagonal 1 : 4 + 5 + 6 = 15

Sum of elements in Diagonal 2 : 8 + 5 + 2 = 15

Hence, a order 3 magic square with elements from 1 to 9 is generated.

### C Program to generate an odd order magic square

This is a C program to implement the above procedure. Just give the order of the magic square required as input. The programs forms a magic square of given order using the above procedure and displays it.

**Program**

#include<stdio.h>

#include<conio.h>

int a,b,A[55][55],K,L,M;

void formmagicsquare(int row,int col,int num)

{

A[row][col]=num;

if(row!=(K-1)&&col!=(K-1))

(A[row+1][col+1]==0)?(row++,col++):(col--);

else if(row==(K-1)&&col!=(K-1))

(A[0][col+1]==0)?(row=0,col++):(col--);

else if(col==(K-1)&&row!=(K-1))

(A[row+1][0]==0)?(col=0,row++):(col--);

else

col--;

if(num<=(K*K))

formmagicsquare(row,col,(A[row][col]==0)?(num+1):(A[row][col]));

}

void main(void)

{

clrscr();

printf("\n Enter a valid odd order:");

scanf("%d",&K);

for(L=0;L<K;L++)

for(M=0;M<K;M++)

A[L][M]=0;

formmagicsquare(K/2,K-1,1);

for(a=0,b=K*K;b!=0;a++,b/=10);

printf("\n An order %d MAGIC SQUARE is \n\n",K);

for(L=0;L<K;L++)

{

for(M=0;M<K;M++)

printf(" %*d ",a,A[L][M]);

printf("\n");

}

getch();

}

**Output**

Sample outputs of the above program are as follows.

GoldPoints : 2