John Delmark Aguilar

Delmark - Profiling
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Magic Square Java

View previous topic View next topic Go down 
AuthorMessage
dElmARk
Admin
Admin
avatar

Posts : 92
Join date : 09/04/2012

PostSubject: Magic Square Java   Tue Oct 23, 2012 1:17 pm

Magic Square Java

Code:

import java.util.*;
class magicSquare{
    public static void main(String[]args)  {
          Scanner del = new Scanner(System.in);
          System.out.print("Input Number: ");
          int N = del.nextInt();
          int square[][] = new int[N][N];
          System.out.println();
          square [0][(N -1) / 2] = 1;
          int key = 2, i = 0, j=(N-1)/2;
          while (key<=N *N)  {
              int k = i-1;
              if(k<0)
                    k+= N;
              int l = j-1;
              if(l<0)
                    l+= N;
              if(square[k][l]!=0) 
                    i=(i+1)% N;
              else
              {
                    i=k;
                    j=l;
              }
              square[i][j] = key;
              key++;
          }
          for(i=0; i<N; i++){
              for(j = 0; j<N; j++)
                    System.out.print(square [i][j] + "\t");
              System.out.println();
          }
    }
}
Back to top Go down
View user profile http://delmark.meabout.com
 
Magic Square Java
View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
John Delmark Aguilar :: dElmARk Productions :: Java Programs :: Tutorials-
Jump to: