# Magic Square

• December 31st, 2013, 04:22 AM
aspic
Magic Square
need help in an assignment
here is what i got so far! after this im having a hard time figuring it out!

here is my code:

public class Start {

public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int num = 1;
int input;
System.out.println("Enter number:");
input = sc.nextInt();
int limit = input;
int a = input/2;
int b = 0;
int [][] arr = new int[input][input];
do{
for( num = 1; num<=limit; num++){
if(arr[b][a]==0){
arr[b][a] = num;
if(b == 0){
b = input;
}
if(a == input -1){
a=-1;

}
a = a+1;
b = b-1;

}

else{
b=b+2;
a = a-1;
}
if(b == input){
b = 0;
}
if(b > input){
b =1;
}
if(a == -1){
a = input-1;
}
arr[b][a] = num;

}
num++;
}while(num <= limit);

for(int x =0;x<input;x++){
for(int y = 0;y<input;y++){
System.out.print(arr[x][y]);

}
System.out.println(" ");
}

}
}
• December 31st, 2013, 04:38 AM
GregBrannon
Re: Magic Square
Welcome to the forum! Please read this topic to learn how to post code correctly along with other useful info for newcomers.

Here's some general guidance that may or may not be included in the link I've given you: Don't just post broken or useless code and expect us to figure out what it's supposed to do and then fix it for you. Tell us what's wrong with it, post any errors you're getting that you can't fix yourself, describe the code's current behavior and how that needs to change, ask specific questions.