I'm a beginner self-taught Java programmer working my way through the Deitel & Deitel examples. I'm on the methods chapter, and it's asking me to come up with a formula to find any perfect numbers between 1 and 1000(sum of factors = number). The problem is it identifies the perfect numbers (6,28...) correctly but gives me others as well (24 for example). Here's the code:

package random; public class PerfectNumber { public void numbers() { for (int i = 1; i < 1000; i++) { int num = i; isPerfect(num); } } public void isPerfect(int num) { int total = 0; for (int i = 1; i < num; i++) { if (num % i == 0) { total += i; if (total == num) { System.out.println(num + ""); } } } } public static void main(String[] args) { PerfectNumber pn = new PerfectNumber(); pn.numbers(); } }

With the results:

6 24 28 496

What am I doing wrong?