Answer to Question #289458 in Java | JSP | JSF for Kev

Question #289458

The Perfectionist's Problem

by CodeChum Admin

I am what you call a perfectionist. I always strive for perfection, and I appreciate everyone and everything that is perfect. That is why I have recently acquired an appreciation for perfect numbers! I absolutely need to know which numbers from 1 to 1000 are considered perfect. From what I recall, a perfect number is a positive integer that is equal to the sum of all its divisors other than itself.


Example:

6 is a perfect number because 6 = 1 + 2 + 3 


1
Expert's answer
2022-01-23T09:41:20-0500
public class Main {
    public static void main(String[] args) {
        int bound = 1000;
        int sum;
        for (int i = 2; i <= bound; i++) {
            sum = 1;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    sum += j;
                }
            }
            if (sum == i) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog