C# Amicable Number Program

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is an Amicable Number?

Two different positive integers a and b form an amicable pair when each equals the other’s proper-divisor sum (aliquot sum), written s(n).

Remember
s(n) = sum of proper divisors of n
a ≠ b
s(a) = b  and  s(b) = a  →  amicable pair

Proper divisors = positive divisors of n, except n itself
Example: s(220) = 284, s(284) = 220 → amicable
ConceptConditionExample
Amicable pairs(a)=b, s(b)=a, a≠b220 & 284
Perfects(n) = n6, 28
Abundants(n) > n12 (not a pair)

Well-known amicable pairs

Golden tests for interviews and unit checks — start with 220 and 284.

List
(220, 284), (1184, 1210), (2620, 2924), (5020, 5564)

How to Check an Amicable Pair in C#

Four clear steps — share one proper-divisor helper for both numbers.

  1. Reject if a < 2, b < 2, or a == b.
  2. Compute s(a) and s(b) (sum proper divisors).
  3. Require s(a) == b and s(b) == a.
  4. Optionally print both sums so the two-way link is visible.
MethodIdeaTimeBest for
Basic loopSum divisors from 1 to n / 2O(a + b)Interviews, small values
Divisor pairsLoop to √n; add i and n / i (skip n)O(√a + √b)Larger inputs, optimizations

Pseudocode

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

function AreAmicable(a, b):
    if a < 2 or b < 2 or a == b:
        return false
    return SumProperDivisors(a) == b and SumProperDivisors(b) == a

Cheat sheet

GoalPattern
Is divisor?n % i == 0
Basic boundfor (i = 1; i <= n / 2; i++)
Amicable tests(a) == b && s(b) == a && a != b
Not perfectReject a == b even if s(a) == a

Live Preview

Enter two positive integers to see s(a), s(b), and the amicable verdict.

Whole numbers ≥ 1 (capped at 999999). Order does not matter for a valid pair.

Result
a = 220, b = 284
s(220) = 284
s(284) = 220
a ≠ b: true
s(a) == b: true
s(b) == a: true
Verdict: AMICABLE PAIR

Worked Walkthrough — 220 & 284

Compute s(220) with a loop to 220 / 2 = 110, then check s(284).

Proper divisors of 220

Divisors addedRunning sum
1, 2, 4, 5, 10, 1133
+ 20, 22, 44, 55, 110284

s(220) = 284 → matches partner b.

Proper divisors of 284

i284 % iActionsum
10Add 11
20Add 23
40Add 47
710Add 7178
1420Add 142220

Final check: s(220) = 284, s(284) = 220, and 220 ≠ 284 → amicable pair (the smallest one).

C# Programs

Three programs: check a fixed pair, user input with both sums printed, and an O(√n) helper. Use View Output for sample results.

Example 1 — Check One Pair

Shared SumProperDivisors helper, then the two-way amicable test.

C#
using System;

class Program
{
    static int SumProperDivisors(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

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

    static bool AreAmicable(int a, int b)
    {
        if (a < 2 || b < 2 || a == b)
        {
            return false;
        }

        return SumProperDivisors(a) == b && SumProperDivisors(b) == a;
    }

    static void Main()
    {
        int a = 220;
        int b = 284;

        if (AreAmicable(a, b))
        {
            Console.WriteLine(a + " and " + b + " are amicable numbers.");
        }
        else
        {
            Console.WriteLine(a + " and " + b + " are not amicable numbers.");
        }
    }
}

How It Works

1. Proper-divisor sum. Loop i from 1 to n / 2 and add every divisor. Never add n itself.

2. Guard the pair. Reject values below 2 and reject a == b so perfect numbers are not treated as amicable.

3. Two-way check. Require s(a) == b and s(b) == a. For 220 and 284 both sides hold.

Example 2 — User Input + Both Sums

Interview follow-up: read two integers safely, print s(a) and s(b), then the verdict.

C#
using System;

class Program
{
    static int SumProperDivisors(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

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

    static void Main()
    {
        Console.Write("Enter first number (a): ");
        if (!int.TryParse(Console.ReadLine(), out int a) || a < 1)
        {
            Console.WriteLine("Please enter a positive integer.");
            return;
        }

        Console.Write("Enter second number (b): ");
        if (!int.TryParse(Console.ReadLine(), out int b) || b < 1)
        {
            Console.WriteLine("Please enter a positive integer.");
            return;
        }

        int sa = SumProperDivisors(a);
        int sb = SumProperDivisors(b);

        Console.WriteLine("s(" + a + ") = " + sa);
        Console.WriteLine("s(" + b + ") = " + sb);

        if (a != b && sa == b && sb == a)
        {
            Console.WriteLine(a + " and " + b + " form an amicable pair.");
        }
        else
        {
            Console.WriteLine(a + " and " + b + " do not form an amicable pair.");
        }
    }
}

How It Works

1. Safe input. int.TryParse avoids FormatException on bad text.

2. Show the sums. Printing s(a) and s(b) makes the link obvious when debugging wrong answers.

3. Same rule. Still require a != b, sa == b, and sb == a. Try 6 and 6: sums match n, but equal inputs are not an amicable pair.

Example 3 — O(√n) Proper-Divisor Helper

Same amicable test; faster sum using divisor pairs up to √n.

C#
using System;

class Program
{
    static int SumProperDivisorsFast(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

        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 partner = n / i;
                if (partner != i && partner != n)
                {
                    sum += partner;
                }
            }
        }

        return sum;
    }

    static bool AreAmicableFast(int a, int b)
    {
        if (a < 2 || b < 2 || a == b)
        {
            return false;
        }

        return SumProperDivisorsFast(a) == b
            && SumProperDivisorsFast(b) == a;
    }

    static void Main()
    {
        Console.WriteLine(AreAmicableFast(220, 284));   // True
        Console.WriteLine(AreAmicableFast(1184, 1210)); // True
        Console.WriteLine(AreAmicableFast(6, 6));       // False (perfect)
    }
}

How It Works

1. Seed with 1. Start the sum at 1 and begin the loop at 2.

2. Walk to √n. While i * i <= n, each factor i has a partner n / i.

3. Add carefully. Always add i. Add partner only when it differs from i and is not n. Same amicable rule as Example 1 — faster sums. Explain the basic helper first in interviews.

Edge Cases & Pitfalls

Check these before calling the solution done.

a == b

Not a pair

Equal inputs are never amicable — that path is for perfect numbers.

One way

Check both

s(a) == b alone is not enough; also require s(b) == a.

Include n?

Never add n

Including n in the sum breaks every amicable and perfect check.

Order

220, 284 = 284, 220

A valid pair is commutative; your function should accept either order.

n < 2

Reject early

Return false when either value is below 2.

Squares

No double-count

In pair mode, add the square root only once when i * i == n.

Time and Space Complexity

ProgramTimeExtra space
Basic loops to n/2O(a + b)O(1)
Divisor pairs up to √nO(√a + √b)O(1)
Scan all pairs in 1…mO(m² · √m) with fast sumO(1)

Key Takeaways

  • Definition: s(a)=b, s(b)=a, and a≠b.
  • Helper first: one proper-divisor sum function powers the whole solution.
  • Two loops: basic 1 … n/2, or pairs up to √n.
  • Test set: (220, 284) true; (6, 6) false; (12, 18) false.

One line: compute both proper-divisor sums; if each equals the other number and they differ, the pair is amicable.

Frequently Asked Questions

Two different positive integers a and b are amicable when the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a. Example: 220 and 284.
s(n) is the aliquot (proper-divisor) sum of n — all positive divisors of n except n itself.
No. Amicable numbers must be different. If s(n) equals n, the number is perfect, not amicable.
s(a) == b alone is not enough. You also need s(b) == a and a != b. One-way matches are not amicable pairs.
Abundant checks one number: s(n) > n. Amicable checks two numbers: s(a) = b and s(b) = a.
Perfect means s(n) = n (one number). Amicable means two different numbers each equal the other's proper-divisor sum.
No. s(1) is 0 in this convention, and amicable pairs start much larger (220, 284).
Write a clear SumOfProperDivisors helper with a loop to n/2, then show the O(√n) pair version if asked to optimize.
Basic loops to n/2 cost O(a + b). Divisor pairs up to √n cost O(√a + √b). Extra space is O(1).
Assert (220, 284) true, (6, 6) false (perfect, not a pair), (12, 18) false, and optionally (1184, 1210) true.

Did you know?

The smallest amicable pair is 220 and 284. Next well-known pairs include 1184 & 1210 and 2620 & 2924.

Next: Armstrong Number

Numbers equal to the sum of their own digits each raised to a power.

Armstrong number tutorial →

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.

12 people found this page helpful