Program to Check if Two Numbers are a Friendly Pair in C
Friendly Pair
A friendly pair (also called an amicable pair) is a pair of numbers where the sum of their proper divisors divided by the number itself results in the same value for both numbers. For example, (220, 284) is a friendly pair.
We will explore a method to check if a given pair of numbers is a friendly pair using C programming.
Method: Using a Function
We calculate the sum of proper divisors for both numbers and check if their ratios match.
#include <stdio.h>
int sumOfDivisors(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int sum1 = sumOfDivisors(num1);
int sum2 = sumOfDivisors(num2);
if ((float)sum1 / num1 == (float)sum2 / num2)
printf("%d and %d are a Friendly Pair", num1, num2);
else
printf("%d and %d are not a Friendly Pair", num1, num2);
return 0;
}
Output:
Enter two numbers: 220 284 220 and 284 are a Friendly Pair