# Thread: need help for magic square

1. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## need help for magic square

here is the 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(" ");
}

}
}

2. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## need help for magic square

this is how i know the code work but definitely there's something wrong..
with the operations and calculations

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(" ");
}

}
}

3. ## Re: need help for magic square

Here's my old entry for the IOCCC; it doesn't even uses arrays for the generation of an odd sized magic square:

Java Code:
```main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}```
kind regards,

Jos (<--- loves clean, readable code)

4. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12

## Re: need help for magic square

lol :) For shame, posting C code in a Java forum.

5. ## Re: need help for magic square

Originally Posted by gimbal2
lol :) For shame, posting C code in a Java forum.
Java is a sissie language; you can't even use it for a proper obfuscation of your stuff; C, otoh, is the mother of all obfuscating languages ;-)

kind regards,

Jos

6. ## Re: need help for magic square

Also posted at: Magic Square

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: need help for magic square

Originally Posted by JosAH
C, otoh, is the mother of all obfuscating languages ;-)
Well, there's APL. Even a well formated APL program looks like someone tried to print a binary file.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•