Java Perfect Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Perfect Number?

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

Remember
n is perfect  ↔  sum(proper divisors of n) == n

Example: 28
  Proper divisors: 1, 2, 4, 7, 14
  Sum = 1 + 2 + 4 + 7 + 14 = 28  ← perfect
nProper-divisor sumResult
61 + 2 + 3 = 6Perfect
281 + 2 + 4 + 7 + 14 = 28Perfect
121 + 2 + 3 + 4 + 6 = 16Abundant (not perfect)
101 + 2 + 5 = 8Deficient (not perfect)

How to Check a Perfect Number in Java

Four clear steps — then pick a simple scan or an O(√n) divisor pairing.

  1. Require n > 1 (1 is not perfect).
  2. Find every divisor of n that is strictly less than n.
  3. Add those divisors together.
  4. Compare the sum with n.
MethodIdeaTimeBest for
Simple loopTest 1 … n-1O(n)Interviews, first explanation
√n pairingAdd i and n/i when they divideO(√n)Faster checks, larger n

Pseudocode

Pseudocode
function IsPerfect(n):
    if n <= 1: return false

    sum = 0
    for i from 1 to n - 1:
        if n mod i == 0:
            sum = sum + i

    return sum == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Proper divisorsPositive divisors excluding n
Testsum == n
1Not perfect
Class familyDeficient / perfect / abundant
First four6, 28, 496, 8128
√n tipAdd both i and n/i; never add n

Live Preview

Enter a positive integer to see its proper divisors, their sum, and the verdict.

Positive integers up to 100000. Tap a chip or type a value.

Result
n = 28
Proper divisors: 1, 2, 4, 7, 14
Sum = 28
28 is a perfect number

Worked Walkthrough — n = 28

Collect every divisor less than 28, then compare the sum with 28.

Calculation
Divisors of 28 less than 28:
  1 divides 28
  2 divides 28
  4 divides 28
  7 divides 28
  14 divides 28

Sum = 1 + 2 + 4 + 7 + 14 = 28
28 == 28 → perfect

(√n view: pairs (1,28), (2,14), (4,7)
 add 1+2+4+7+14 — skip 28 itself)

Java Programs

Start with the simple loop so every proper divisor is obvious. Then use the √n method when you want the same answer faster.

Example 1 — Simple Loop

Sum every divisor from 1 to n - 1, then compare with n.

Java
class PerfectNumber
{
    static boolean isPerfect(int n)
    {
        if (n <= 1)
        {
            return false;
        }

        int sum = 0;
        for (int i = 1; i < n; i++)
        {
            if (n % i == 0)
            {
                sum += i;
            }
        }

        return sum == n;
    }

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

How It Works

  1. Return false for n <= 1 — 1 is not perfect.
  2. Loop i from 1 to n - 1 so n itself is never added.
  3. When n % i == 0, i is a proper divisor — add it to sum.
  4. After the loop, compare sum == n.
  5. For 28, the sum is 28, so the method returns true.

Example 2 — √n Pairing

Walk only up to √n and add both halves of each divisor pair, skipping n.

Java
class PerfectSqrt
{
    static boolean isPerfect(int n)
    {
        if (n <= 1)
        {
            return false;
        }

        int sum = 1; // 1 is always a proper divisor for n > 1
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                sum += i;
                int pair = n / i;
                if (pair != i && pair != n)
                {
                    sum += pair;
                }
            }
        }

        return sum == n;
    }

    public static void main(String[] args)
    {
        System.out.println(isPerfect(6));   // true
        System.out.println(isPerfect(28));  // true
        System.out.println(isPerfect(12));  // false
        System.out.println(isPerfect(10));  // false
    }
}

How It Works

  1. Seed sum = 1 because every n > 1 is divisible by 1.
  2. Loop while i * i <= n so you only scan up to √n.
  3. When i divides n, add i and usually its pair n / i.
  4. Skip the pair when it equals i (perfect square) or equals n (would not be proper).
  5. Same answers as Example 1, in O(√n) time.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

Not perfect

Treat n <= 1 as false before summing.

Include n?

Exclude itself

Proper divisors never include n. Adding it breaks the definition.

√n pair

Do not double-count

When i == n/i, add the square-root divisor once.

Negatives

Positive only

Perfect numbers are defined for positive integers.

Time and Space Complexity

ProgramTimeExtra space
Simple loopO(n)O(1)
√n pairingO(√n)O(1)

Both answers are the same. Prefer √n when n grows beyond small demo values.

Key Takeaways

  • Definition: sum of proper divisors equals n.
  • Exclude: never add n itself to the sum.
  • Family: deficient / perfect / abundant by comparing sum and n.
  • Test set: 6 → yes; 28 → yes; 12 → no; 10 → no.

One line: add every positive divisor of n except n, then check equality.

Frequently Asked Questions

A positive integer equal to the sum of its proper divisors (positive divisors excluding itself). Example: 6 = 1 + 2 + 3.
No. The sum of proper divisors of 1 is usually taken as 0, which is not equal to 1.
Compare the proper-divisor sum s with n: s < n deficient, s == n perfect, s > n abundant.
6, 28, 496, and 8128 are the first four perfect numbers.
A loop to n-1 is O(n). Pairing divisors up to √n is O(√n). Both use O(1) extra space.

Next: Perfect Square

Check whether a number is the square of an integer.

Perfect square tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful