adding old solved challenges
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user