dElmARk Admin
Posts : 92 Join date : 09/04/2012
| Subject: Magic Square Java Tue 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(); } } }
| |
|