Java program to find perfect numbers below 10,000

Chester S. picture Chester S. · Sep 17, 2015 · Viewed 9.4k times · Source

I am currently working on java code that will allow me to print out all perfect numbers below 10,000. My issue is that I can not figure out why my code is not printing 6, but is printing all the other perfect numbers. My code is below, please send help if you can see what I have over looked. Thank you,

int min = 1;
int max = 10000;

for (min = 1; min <= max; min++) {
    int sum = 0;
    int e = 1;
    for (e = 1; e < min; e++) {
        int a = min % e;

        if (a == 0) {
            sum += e;
        }
    } 
    if (sum == min){           
        System.out.println(sum);
    }         
}     

Answer

user5347043 picture user5347043 · Sep 17, 2015

Your solution should be fine, but if still has trouble, try to clear then rebuild.

my code listed below gets the correct answers:

public static void main(String[] args){
    int min = 1; 
    int max = 10000;

    for (min = 1; min <= max; min++) { 
        int sum = 0;
        for (int e = 1; e < min; e++) {
            if ((min % e) == 0) {
                sum += e;
            } 
        }  
        if (sum == min){           
            System.out.println(sum);
        }          
    }      
}