/**
* Write a description of class MagicSquare here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class MagicSquare
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Hello, please enter a number");
int n = sc.nextInt();
int square [] [] = new int[n][n] ;
int key = n*n;
int row = n/2;
int col = n-1;
int counter = 1;
for(int i = 0; i< n; i++){
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(square[row][col]!=0){
col = col- 2;
row++;
}
square[row][col] = counter;
counter++;
row--;
col++;
}
}
for (int r = 0; r < square.length; r ++) {
for (int c =0; c < square.length; c ++) {
System.out.print(square[r][c] + " ");
}
System.out.println("");
}
}
}
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.