I have an assignment to find all the “magic numbers” within a range of numbers. A magic number is a number whose factors sums up to that number. My code below is not printing out the magic number. Can anyone help me with this.
public class MagicNumber {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("What is the top of the range?");
int range = IO.readInt();
if (range <= 0 ) {
IO.reportBadInput();
}
int sumOfFactors = 0;
for (int i = 1 ; i <= range ; i++) {
for (int m = 1 ; m < i; m++) {
if (i % m == 0) {
sumOfFactors = sumOfFactors + m;
}
if (sumOfFactors == i) {
System.out.println(i);
}
}
}
}
}