Check Perfect Number in C#

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

What you’ll learn

  • What a perfect number is: sum of proper divisors equals the number itself.
  • How to check 28 using the sqrt divisor-pair optimization from the reference.
  • How to list perfect numbers from 1 to 50 with a simple loop to n / 2.
  • Live preview showing perfect, abundant, or deficient verdicts.

What is a perfect number?

A positive integer is perfect when the sum of its proper divisors (all divisors except itself) equals the number. Example: divisors of 6 are 1, 2, 3 and 1 + 2 + 3 = 6.

Classic example

28 = 1 + 2 + 4 + 7 + 14 — perfect.

Live preview

See divisor sum and perfect / abundant / deficient label.

Range scan

From 1 to 50 only 6 and 28 qualify.

Prerequisites

Divisors, modulo (%), loops, and optional Math.Sqrt.

  • You know that n % i == 0 means i divides n.
  • You can accumulate a running sum inside a for loop.

The idea

Add every proper divisor of n. If the total equals n, the number is perfect. If the total is greater, it is abundant; if less, deficient.

Perfect sum = n
Abundant sum > n
Deficient sum < n

Definition

Let s(n) be the sum of proper divisors of n. Then n is perfect when s(n) = n. Proper divisors exclude n itself.

Example: 28

s(28) = 1 + 2 + 4 + 7 + 14 = 28, so 28 is perfect.

Intuition examples

6 → 1+2+3=6 (perfect). 12 → 1+2+3+4+6=16 (abundant). 10 → 1+2+5=8 (deficient).

Live preview

Type a positive integer and see proper divisors, their sum, and whether the number is perfect, abundant, or deficient.

Live result
Press “Run check”.

Algorithm

Goal: decide whether n is perfect.

Reject small values

If n <= 1, return false1 is not perfect.

Sum proper divisors

Loop divisors and add them (simple: 1..n/2; faster: pairs up to sqrt(n)).

Compare

Return true when sum == n.

📜 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 (sqrt optimization)

Reference approach: start sum = 1, loop to Math.Sqrt(number), add divisor pairs. Tests 28.

c#
using System;

class Program
{
    static bool IsPerfectNumber(int number)
    {
        if (number <= 1)
            return false;

        int sum = 1;

        for (int i = 2; i <= Math.Sqrt(number); i++)
        {
            if (number % i == 0)
            {
                if (i == number / i)
                    sum += i;
                else
                    sum += i + (number / i);
            }
        }

        return sum == number;
    }

    static void Main()
    {
        int number = 28;

        if (IsPerfectNumber(number))
            Console.WriteLine($"{number} is a perfect number.");
        else
            Console.WriteLine($"{number} is not a perfect number.");
    }
}

How the program works

  1. 1 is always counted first as a proper divisor.
  2. When i divides number, add i and number/i as a pair (or just i when it is a square root).
  3. Compare the accumulated sum to the original — equal means perfect.
2

Perfect numbers from 1 to 50

Beginner-friendly loop to n / 2 — same logic as the reference range program. Only 6 and 28 appear in this interval.

c#
using System;

class Program
{
    static bool IsPerfectNumber(int num)
    {
        if (num <= 1)
            return false;

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

        return sum == num;
    }

    static void Main()
    {
        Console.WriteLine("Perfect Numbers in the range 1 to 50:");

        for (int i = 1; i <= 50; i++)
        {
            if (IsPerfectNumber(i))
                Console.Write($"{i} ");
        }

        Console.WriteLine();
    }
}

Optimization notes

Sqrt pairs. Example 1 visits about O(√n) candidates instead of O(n) — ideal for single checks in interviews.

Use long for sums. When scanning large ranges, divisor sums can exceed int.MaxValue.

❓ FAQ

A positive integer n is perfect when the sum of its proper divisors (all divisors except n itself) equals n.
No. The only proper divisor of 1 would be none, so the sum is 0, not 1.
1 is always a proper divisor of n > 1. The loop from 2 finds paired divisors; 1 is added separately.
No proper divisor of n can exceed n/2, so checking 1 through n/2 is enough.
If divisor sum equals n, it is perfect; greater than n, abundant; less than n, deficient.
The simple loop is O(n). The sqrt pair method is O(sqrt(n)).

🔄 Input / output examples

nProper divisor sumVerdict
61+2+3=6Perfect
281+2+4+7+14=28Perfect
101+2+5=8Deficient
121+2+3+4+6=16Abundant

Edge cases and pitfalls

n = 1

Not perfect

Without the n <= 1 guard, the sqrt method wrongly treats 1 as perfect.

Square root

Perfect squares

When i == number / i, add i only once — not twice.

Overflow

Large n

Use long for sum when testing bigger values.

⏱️ Time and space complexity

MethodTimeExtra space
Loop to n / 2O(n)O(1)
Sqrt divisor pairsO(√n)O(1)
Range 1..RO(R × cost per check)O(1)

Summary

  • Perfect means proper-divisor sum equals n.
  • Simple loop to n/2 is easy; sqrt pairs are faster.
  • Guard n <= 1 and watch sum overflow on large inputs.
Did you know?

The first four perfect numbers are 6, 28, 496, and 8128. Only about 51 perfect numbers are known — they are extremely rare.

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