Perfect Number in Java
Last updated: February 8, 2025
A perfect number is one of the most interesting concepts in number theory and is frequently asked in Java programming assignments and interviews. In this article, we will explore perfect numbers in detail and write multiple Java programs to determine whether a number is perfect.
What is a Perfect Number?
A perfect number is a number that is equal to the sum of its proper divisors (excluding the number itself).
Examples of perfect numbers:
6, 28, 496, 8128
Example: For number 6 → divisors are 1, 2, and 3. Sum = 1 + 2 + 3 = 6 → So, 6 is a perfect number.
Java Program: Check Perfect Number Using Loop
import java.util.Scanner;
public class PerfectNumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n)
System.out.println(n + " is a Perfect Number.");
else
System.out.println(n + " is NOT a Perfect Number.");
}
}
Perfect Number Using a Function
A cleaner and modular approach using a separate function.
public class PerfectNumberFunction {
public static boolean isPerfect(int n) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0)
sum += i;
}
return sum == n;
}
public static void main(String[] args) {
int n = 28;
if (isPerfect(n))
System.out.println(n + " is a Perfect Number.");
else
System.out.println(n + " is NOT a Perfect Number.");
}
}
Optimized Perfect Number Check
Instead of checking up to n/2, we check up to √n for performance improvement.
public class PerfectNumberOptimized {
public static boolean isPerfect(int n) {
int sum = 1;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum == n && n != 1;
}
public static void main(String[] args) {
int n = 496;
if (isPerfect(n))
System.out.println(n + " is Perfect.");
else
System.out.println(n + " is Not Perfect.");
}
}
Print All Perfect Numbers in a Range
This program prints all perfect numbers between 1 and 10,000.
public class PerfectNumberRange {
public static boolean isPerfect(int n) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0)
sum += i;
}
return sum == n;
}
public static void main(String[] args) {
System.out.println("Perfect numbers between 1 and 10000:");
for (int i = 1; i <= 10000; i++) {
if (isPerfect(i))
System.out.print(i + " ");
}
}
}
Interesting Facts About Perfect Numbers
- All known even perfect numbers follow the formula:
2^(p−1)(2^p − 1) 2^p − 1must be a Mersenne Prime- There are only 51 known perfect numbers as of today
- No odd perfect number has been discovered yet
Conclusion
Perfect numbers are an important part of number theory and a favorite topic in Java programming exercises. We explored multiple approaches—from simple loops to optimized divisor checks—to find perfect numbers in Java. Try writing programs for checking near-perfect or abundant numbers to deepen your understanding.