/* Magic square in 3x3 square matrix
fill 1 in middle column of the first row
Loop till all the valles are filled
    move 1 up and 1 right 
    if not blank move move 1 up and 1 left
    fill the next number
End Loop
*/
public class MagicSquareGenerate3x3
{   public static void main()
    {   int a[][]=new int[3][3];
        int r=0, c=a.length/2;
        for(int n=1; n<=a.length*a.length; n++)
        {   a[r][c]=n;
            r--; if(r==-1) r=a.length-1;
            c++; if(c==a.length) c=0;
            if(a[r][c]!=0)
            {   r--; if(r==-1) r=a.length-1;
                c--; if(c==-1) c=a.length-1;
            }
        }
        for(int i=0; i<a.length; i++)
        {   for(int j=0; j<a.length; j++)
            {   System.out.print(a[i][j]+"\t");
            }//for
            System.out.println();
        }//for
    }//main
}//class

