23 lines
566 B
Java
23 lines
566 B
Java
public class ThePerfectNumber {
|
|
public static void main(String[] args) {
|
|
int[] randNums = { 2, 5, 6, 15, 23, 26, 34, 53, 343, 2454, 28 };
|
|
for (int number : randNums) {
|
|
if (isPerfect(number)) {
|
|
System.out.println(number + " is a perfect number");
|
|
} else {
|
|
System.out.println(number + " is NOT a perfect number");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static boolean isPerfect(int num) {
|
|
int total = 0;
|
|
for (int i = 1; i <= num / 2; i++) {
|
|
if (num % i == 0) {
|
|
total += i;
|
|
}
|
|
}
|
|
return total == num;
|
|
}
|
|
}
|