Check Perfect Number in Java

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Divisor sum

What you’ll learn

  • How to decide if a number is perfect using proper divisors.
  • How to implement single-check and range-check programs in Java.
  • How perfect, abundant, and deficient numbers differ.

What is a perfect number?

A positive integer is perfect when the sum of its proper divisors equals the number itself.

Example: for 6, proper divisors are 1, 2, 3 and 1 + 2 + 3 = 6, so 6 is perfect.

Prerequisites

Divisors, modulo checks, and simple loop-based accumulation.

  • Divisors and modulo operation.
  • Looping from 1 to n/2.

The idea

Add all proper divisors of n. If the sum equals n, it is perfect.

Definition

A number is perfect if the sum of its proper divisors equals the number: s(n) = n.

Intuition examples

6 -> 1+2+3=6 (perfect), 12 -> 1+2+3+4+6=16 (abundant).

Live preview

Type a value and check whether it is perfect, abundant, or deficient.

Live result
Press "Run check".

Algorithm

Initialize sum

Start with sum = 0.

Scan divisors

Loop i from 1 to n / 2.

Add proper divisors

If n % i == 0, add i to sum.

Compare

If sum == n, number is perfect.

📜 Pseudocode

Pseudocode
function isPerfect(n):
  if n <= 1:
    return false
  sum = 0
  for i from 1 to floor(n / 2):
    if n mod i == 0:
      sum = sum + i
  return sum == n
1

Check one number

This example checks whether 28 is perfect.

java
public class Main {
    static boolean isPerfectNumber(int number) {
        if (number <= 1) return false;
        int sum = 0;
        for (int i = 1; i <= number / 2; i++) {
            if (number % i == 0) {
                sum += i;
            }
        }
        return sum == number;
    }

    public static void main(String[] args) {
        int number = 28;
        if (isPerfectNumber(number)) {
            System.out.println(number + " is a perfect number.");
        } else {
            System.out.println(number + " is not a perfect number.");
        }
    }
}
2

Perfect numbers in a range

This prints all perfect numbers from 1 to 50.

java
public class Main {
    static boolean isPerfectNumber(int number) {
        if (number <= 1) return false;
        int sum = 0;
        for (int i = 1; i <= number / 2; i++) {
            if (number % i == 0) {
                sum += i;
            }
        }
        return sum == number;
    }

    public static void main(String[] args) {
        System.out.println("Perfect numbers between 1 and 50:");
        for (int i = 1; i <= 50; i++) {
            if (isPerfectNumber(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Optimization note

You can iterate to sqrt(n) and add divisor pairs to reduce time.

❓ FAQ

A positive integer n is perfect when the sum of its proper divisors equals n.
No. For 1, the proper-divisor sum is 0, so it is not perfect.
No proper divisor can be greater than n/2, so checking up to n/2 is enough.
If divisor sum is equal to n => perfect, greater than n => abundant, less than n => deficient.
Checking one number with loop 1 to n/2 takes O(n) time and O(1) extra space.
Yes. You can iterate up to sqrt(n) and add divisor pairs to get about O(sqrt(n)) time.

🔄 Input / output examples

nVerdict
28Perfect
10Not perfect
6Perfect

Edge cases

Lower bound

1 is not perfect

Its proper-divisor sum is 0, not 1.

Overflow

Large values

For larger ranges, use long to avoid sum overflow.

⏱️ Time and space complexity

TaskTimeExtra space
Check one numberO(n)O(1)
Check one number with sqrt optimizationO(√n)O(1)

Summary

  • Compute proper-divisor sum and compare with n.
  • This direct method is O(n); sqrt-pairing improves it.
Did you know?

The first four perfect numbers are 6, 28, 496, and 8128.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful