JustPerfects.java (for Loop).
In this program, you will use loops to identify perfect numbers (a number equal to the sum of its divisors - 6 and 28 are the first two! 6 is 2 +3 + 1 and 28 is 14+7+4+2+1). Being the perfect number is quite unique. Only 4 perfect numbers exist between 1 and 10,000 and you’ve already found two! Please write a program to find the additional pair.
Bonus: How many perfect numbers exist between 10,000 and 35,000? (+2)
public class JustPerfects { public static void main(String[] args) { for (int i = 1; i <= 10000; i++) { int sum = 0; for (int j = 1; j < i; j++) { if (i % j == 0) sum += j; } if (sum == i) { System.out.println(i); } } int count = 0; for (int i = 10000; i <= 35000; i++) { int sum = 0; for (int j = 1; j < i; j++) { if (i % j == 0) sum += j; } if (sum == i) { ++count; } } System.out.println("Number of perfect numbers between 10,000 and 35,000 is " + count); } }
Get Answers For Free
Most questions answered within 1 hours.