Perfect Number Check in Java: Explanation & Practice
Check if a number is perfect
Problem summary
A perfect number equals the sum of its proper divisors (excluding itself). Check if the given number is perfect.
Starter code
public class Main {
public static void main(String[] args) {
int number = 28; // Test case 1
// Check if perfect: 28 = 1+2+4+7+14
// Print: Is perfect: <true/false>
}
}Expected output and test cases
- 28 = 1+2+4+7+14
Is perfect: true
- 6 = 1+2+3
Is perfect: true
- 12 is not perfect
Is perfect: false
Hints
- Find all divisors from 1 to n/2
- A divisor divides evenly (remainder = 0)
- Sum the divisors and compare with original
Related Core Java Basics exercises
- Practice Least Common Multiple in Java
- Practice Armstrong Number Check in Java
- Practice Number to Words in Java
Practice all Core Java Basics exercises · Run this idea in the Java compiler