Logic Problem, I'm new to java but have experience with many other
languages. I've been working on an exercise, but one or two answers that my
program is generating have me puzzled. The program is to list the perfect
numbers between 1 and 1000. Perfect numbers are numbers whos
factors(excluding itself) can be added to equal the original number. I keep
getting an output of 6, 24, 28, and 496. The 24 has me stumped. Where is
my logic error? The program is below:
public class Perfect
{
public static void main(String[] args)
{
int count;
int total;
int num;
System.out.print("Perfect numbers between 1 and 1000 are: ");
for (count = 1; count < 1000; ++count)
{
total = 0;
for (num = 1; num < count; ++num)
{
if(count%num == 0)
{
total = total + num;
}
if(total == count)
{
System.out.print(count + " ");
num = count;
}
}
}
}
}