if (sum == number) check needs to be done outside the loop. Otherwise you might pick up numbers such that the sum of a subset of divisors equals the number.
In fact, 24 is one such example since
1+2+3+4+6+8=24. Your code prematurely concludes that 24 is perfect, despite it also being divisible by 12.