Java Program to print MAGIC Square – Q15
Q15: Java Program to print MAGIC Square
class magicSq{ public static void main(String args[]){ int l, i, j, x, y, val=1; l = Integer.parseInt(args[0]); int mat[][] = new int[l][l]; x = 0; y = l/2; mat[x][y] = val++; while(val <= l*l){ x--; y--; if((x < 0) && (y < 0)){ x = 1; y = 0; } if(x < 0) x = l-1; if(y < 0) y = l-1; if(mat[x][y] != 0){ x+=2; y++; } mat[x][y] = val++; System.out.println(x + " " + y + " " + mat[x][y]); } for(i=0; i<l; i++){ for(j=0; j<l; j++){ System.out.print(" " + mat[i][j]); } System.out.println(); } } }
… from College notes (BCA/MCA assignments):
Categories: Java
Java Basic Programs, Magic Square
Comments (0)
Trackbacks (0)
Leave a comment
Trackback