MagicSquare

/** * Write a description of class MagicSquare here. * * @author (your name) * @version (a version number or a date) */ import java.util.*; public class Square { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number"); int n = scan.nextInt(); int mag [] [] = new int[n][n] ; int row = n/2; int col = n-1; int count = 1; int square = n*n; 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; } else if( row <0){ row =n-1; } if(mag[row][col]!=0){ col = col- 2; row++; } mag[row][col] = count; count++; row--; col++; } } for (int a = 0; a < mag.length; a++) { for (int f =0; f < mag.length; f++) { System.out.print(mag[a][f] + " "); } 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.