John Delmark Aguilar
Would you like to react to this message? Create an account in a few clicks or log in to continue.
John Delmark Aguilar

Personal Forum
 
HomeSearchLatest imagesRegisterLog in

 

 Magic Square Java

Go down 
AuthorMessage
dElmARk
Admin
Admin
dElmARk


Posts : 92
Join date : 09/04/2012

Magic Square Java Empty
PostSubject: Magic Square Java   Magic Square Java Icon_minitimeTue Oct 23, 2012 8: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
https://delmark.forumtl.com
 
Magic Square Java
Back to top 
Page 1 of 1
 Similar topics
-
» Java Comments
» Payroll Java
» Java Programming Index
» Compiling Java Program
» List Of Java Keywords

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