C Amicable Number Program

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

What Are Amicable Numbers?

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

Remember
s(n) = sum of proper divisors of n

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

Classic pair: 220 and 284
  s(220) = 284
  s(284) = 220
abResult
220284Amicable
11841210Amicable
2828Not amicable (perfect)
220221Not amicable

How to Check Amicable Numbers in C

Four clear steps — both directions matter.

  1. Reject the case a == b (that belongs to perfect numbers).
  2. Compute s(a) = sum of proper divisors of a.
  3. Compute s(b) the same way.
  4. Accept only when s(a) == b and s(b) == a.
MethodIdeaTimeBest for
Loop to n/2Clear proper-divisor sumO(a+b)Interviews, beginners
Pairs to √nFaster s(n)O(√a+√b)Larger inputs
Find partnerb = s(a), verify reverseO(√a)Given one number

Pseudocode

Pseudocode
function SumProper(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 SumProper(a) == b and SumProper(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 <stdio.h>

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

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

    return sum;
}

int areAmicable(int num1, int num2) {
    if (num1 == num2) {
        return 0;
    }
    return sumOfDivisors(num1) == num2 && sumOfDivisors(num2) == num1;
}

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

    if (areAmicable(a, b)) {
        printf("%d and %d are amicable numbers.\n", a, b);
    } else {
        printf("%d and %d are not amicable numbers.\n", a, b);
    }

    return 0;
}

How It Works

  1. sumOfDivisors adds every divisor strictly less than num.
  2. areAmicable returns 0 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 <stdio.h>

/* Sum of proper divisors s(n); s(1) = 0 */
int sumOfDivisors(int num) {
    if (num <= 1) {
        return 0;
    }

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

    return sum;
}

int areAmicable(int num1, int num2) {
    if (num1 == num2) {
        return 0;
    }
    return sumOfDivisors(num1) == num2 && sumOfDivisors(num2) == num1;
}

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

    if (areAmicable(a, b)) {
        printf("%d and %d are amicable numbers.\n", a, b);
    } else {
        printf("%d and %d are not amicable numbers.\n", a, b);
    }

    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 <stdio.h>

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

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

    return sum;
}

/* Returns partner if amicable, otherwise 0 */
int amicablePartner(int a) {
    int b = sumOfDivisors(a);
    if (a != b && sumOfDivisors(b) == a) {
        return b;
    }
    return 0;
}

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

    if (partner != 0) {
        printf("Partner of %d is %d.\n", a, partner);
    } else {
        printf("%d has no amicable partner.\n", a);
    }

    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 0 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: proper-divisor sum — same building block as perfect/abundant.
  • Test set: (220,284) yes; (28,28) no; (220,221) no.

One line: each number is the proper-divisor sum of the other — and they must be different.

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 digit powers.

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