Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help with a magic square program

807603Jan 24 2008 — edited Jan 27 2008
Hi, I have to write a code that prints out first 10 magic square numbers
-- magic square numbers: numbers that is a perfect square and sum of consecutive integers (like 36 -- 6 squared or sum of 1 to 8)

here's my code:

public class Loop {

public static void magicSquare ()
{
long inc = 0;
long integer = 0;
long total = 0;

for (int square =1; square>0; square++)
{
total = square*square;
int sum = 0;
for (int i = 1; i>0; i++)
{
if (sum<total){
sum += i;
}
else if (sum==total){
System.out.println("Magic Square is: " + total);
break;
}
else {
break;
}
}
}
}
}

I have a simple seperate driver
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2008
Added on Jan 24 2008
11 comments
290 views