public class MagicSquare
{
public MagicSquare(int n)
{
int [][] ms = new int [n][n];
int row = n/2;
int col = n-1;
int counter = 1;
for(int r=0; r<n; r++){
for(int c = 0; c<n; c++){
if(row==-1 && col==n){
row = 0;
col = n-2;
}else{
if(col>=n){
col=0;
}
if(row<0){
row=n-1;
}
}
if(ms[row][col] !=0){
col = col - 2;
row++;
}
ms[row][col]=counter;
row --;
}
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.