Page 595 - Computer science 868 Class 12
P. 595

4       int n,a[][];
                   5       Scanner sc=new Scanner(System.in);

                   6       magic_square()
                   7       {
                   8       n=0;

                   9       }

                   10      void magic()
                   11      {
                   12      System.out.print("Enter Size:");

                   13      do
                   14      {

                   15      n=sc.nextInt();
                   16      }

                   17      while(n%2==0);  // checking for odd
                   18      a=new int[n][n];

                   19      int s=n*n,m=n/2,r=0,c=m,x=1;
                   20      a[r][m]=x;

                   21      x++;
                   22      while(x<=s)

                   23      {
                   24      r=r-1;

                   25      c++;
                   26      if(r<0&&c==n)  // right corner

                   27      {
                   28      r=r+2;

                   29      c--;
                   30      }

                   31      else
                   32      if(r<0)  // when row<0
                   33      {

                   34      r=n-1;

                   35      }
                   36      else
                   37      if(c==n)  // when column=size





                                                                                                                       593
                                                                                                   Internal Assessment  593
   590   591   592   593   594   595   596   597   598   599   600