C++ Amicable Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is an Amicable Number?

Two different positive integers a and b form an amicable pair when each is the sum of the other’s proper divisors.

Remember
a and b are amicable  ↔  s(a) = b  and  s(b) = a  and  a ≠ b

s(n) = sum of proper divisors of n
     = positive divisors of n except n itself

Classic pair: 220 and 284
  s(220) = 284
  s(284) = 220
abs(a)s(b)Result
220284284220Amicable
1184121012101184Amicable
28282828Perfect (not a pair)
2202212841Not amicable

How to Check Amicable Numbers in C++

Compute both proper-divisor sums, then verify the cross equalities.

  1. Reject the case a == b (that would be perfect, not amicable).
  2. Compute s(a) and s(b) (sum of divisors strictly less than the number).
  3. Return true only when s(a) == b and s(b) == a.
MethodIdeaTimeBest for
Loop to n/2Clear s(n), then check both waysO(a + b)Interviews, clarity
√n pairsFaster s(n)O(√a + √b)Larger inputs
Find partnerb = s(a), verify s(b) == aO(a) or O(√a)One known value

Pseudocode

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

function AreAmicable(a, b):
    if a == b:
        return false
    return ProperDivisorSum(a) == b and ProperDivisorSum(b) == a

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Rules(a)=b and s(b)=a, a≠b
s(n)Sum of divisors strictly less than n
Classic pair(220, 284)
EqualsNot amicable — check perfect instead
One directionNot enough — verify both ways
Partner of ab = s(a), then check s(b)=a

Live Preview

Enter two positive integers to compare each proper-divisor sum with the other value.

Positive whole numbers. Tap a chip or type a pair.

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

Worked Walkthrough — 220 and 284

Compute each proper-divisor sum, then check both cross equalities.

Calculation
Proper divisors of 220:
  1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
  Sum = 284

Proper divisors of 284:
  1, 2, 4, 71, 142
  Sum = 220

220 ≠ 284, s(220)=284, s(284)=220
→ amicable pair

C++ Programs

Start with the clear n/2 sum. Then speed it up with √n pairs, and finish by finding a partner from one value.

Example 1 — Basic Pair Check

Sum proper divisors with a loop to num / 2, then test both directions.

C++
#include <iostream>

int properDivisorSum(int num) {
    if (num <= 1) {
        return 0;
    }

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

bool areAmicable(int a, int b) {
    if (a == b) {
        return false;
    }
    return properDivisorSum(a) == b && properDivisorSum(b) == a;
}

int main() {
    int a = 220;
    int b = 284;

    if (areAmicable(a, b)) {
        std::cout << a << " and " << b << " are amicable numbers.\n";
    } else {
        std::cout << a << " and " << b << " are not amicable numbers.\n";
    }

    return 0;
}

How It Works

  1. properDivisorSum adds every divisor strictly less than num.
  2. areAmicable returns false when both inputs are equal.
  3. It then requires s(a) == b and s(b) == a.
  4. For 220 and 284 both cross equalities hold.
  5. This is the clearest interview version.

Example 2 — Optimized with Divisor Pairs

Walk i up to √n and add both factors (skipping n itself).

C++
#include <iostream>

int properDivisorSum(int num) {
    if (num <= 1) {
        return 0;
    }

    int total = 1;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            total += i;
            int pair = num / i;
            if (pair != i) {
                total += pair;
            }
        }
    }
    return total;
}

bool areAmicable(int a, int b) {
    if (a == b) {
        return false;
    }
    return properDivisorSum(a) == b && properDivisorSum(b) == a;
}

int main() {
    int a = 220;
    int b = 284;

    if (areAmicable(a, b)) {
        std::cout << a << " and " << b << " are amicable numbers.\n";
    } else {
        std::cout << a << " and " << b << " are not amicable numbers.\n";
    }

    return 0;
}

How It Works

  1. Seed the sum with 1 for every n > 1.
  2. Loop while i * i <= num and add each factor pair.
  3. When i == num / i, add the square root only once.
  4. Partner num / i is never num itself because i starts at 2.
  5. The amicable check is unchanged — only s(n) is faster.

Example 3 — Find Amicable Partner of a

Compute b = s(a), then confirm s(b) == a and a != b.

C++
#include <iostream>

int properDivisorSum(int num) {
    if (num <= 1) {
        return 0;
    }

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

// Returns the partner, or -1 when none exists.
int amicablePartner(int a) {
    int b = properDivisorSum(a);
    if (a != b && properDivisorSum(b) == a) {
        return b;
    }
    return -1;
}

int main() {
    int a = 220;
    int partner = amicablePartner(a);

    if (partner != -1) {
        std::cout << "Partner of " << a << " is " << partner << ".\n";
    } else {
        std::cout << a << " has no amicable partner.\n";
    }

    return 0;
}

How It Works

  1. The only candidate partner is b = s(a).
  2. Reject the case a == b (perfect numbers).
  3. Verify the reverse: s(b) == a.
  4. Return b on success, or -1 when no partner exists.
  5. For 220, the partner is 284.

Edge Cases & Pitfalls

Check these before calling the solution done.

Equals

Not amicable

If a == b, you are looking at a perfect-number test, not a pair.

One way

Check both directions

s(a) == b alone is not enough — you also need s(b) == a.

s(n)

Exclude n itself

Proper divisors never include the number. Including it breaks every check.

1

s(1) = 0

1 has no proper divisors under the usual convention. Guard n <= 1.

Time and Space Complexity

ProgramTimeExtra space
Basic pair checkO(a + b)O(1)
Sqrt pair checkO(√a + √b)O(1)
Find partnerO(a) or O(√a)O(1)

Explain the simple loop first; mention √n when asked about larger inputs.

Key Takeaways

  • Definition: s(a)=b, s(b)=a, and a≠b.
  • Classic: 220 ↔ 284.
  • Tool: reuse s(n) for perfect, abundant, and amicable checks.
  • Trap: one-way equality or a == b is not amicable.

One line: each number’s proper-divisor sum must equal the other — and they must differ.

Frequently Asked Questions

Two different positive integers a and b are amicable if the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a. The smallest pair is (220, 284).
No. That would mean s(a) = a, which is a perfect number, not an amicable pair. The definition requires two distinct numbers.
s(n) is the sum of proper divisors of n — all positive divisors of n that are smaller than n.
All three use s(n). Perfect: s(n)=n. Abundant: s(n)>n. Amicable: s(a)=b and s(b)=a with a≠b.
A clear loop to n/2 per value is O(a+b). Using divisor pairs up to √n makes one pair check O(√a + √b).

Next: Armstrong Number

Check whether a number equals the sum of its digits raised to the digit count.

Armstrong number 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.

18 people found this page helpful