Java program to find perfect numbers below 10,000 [closed]

▼魔方 西西 提交于 2020-06-29 05:32:32

问题


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);
    }         
}     

回答1:


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);
        }          
    }      
} 


来源:https://stackoverflow.com/questions/32633892/java-program-to-find-perfect-numbers-below-10-000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!