Friendly Pair in C++
C Program to check Friendly Pair or not
We will write a code to Check Friendly pair in C++
Two numbers num1 & num2 are friendly pairs if the following holds true - (Sum of divisors of num1)/num1= (Sum of divisors of num2)/num2
Algorithm:-
For two numbers num1 and num2
- Create two variables, sum1 and sum2
- Find the sum of divisors for num1 and num2 and assign the respective sums to sum1 and sum2 variables
- Find the ratio of sum1/num1 and sum2/num2
- If both ratios are same then these are friendly pair numbers
Method 1
C++ Code:-
Run
#include <iostream>
using namespace std;
int getDivisorsSum(int num){
int sum = 0;
for(int i = 1; i < num; i++){
if(num % i == 0)
sum = sum + i;
}
return sum;
}
int main ()
{
int num1 = 30, num2 = 140;
int sum1 = getDivisorsSum(num1);
int sum2 = getDivisorsSum(num2);
if(sum1/num1 == sum2/num2)
cout << num1 << " & " << num2 << " are friendly pairs";
else
cout << num1 << " & " << num2 << " are not friendly pairs";
}
// Time complexity: O(N)
// Space complexity: O(1)
Output
30 & 140 are friendly pairs
Method 2
C++ Code:-
Run
#include <iostream>
#include <math.h>
using namespace std;
int getDivisorsSum(int n){
int sum = 0;
for(int i = 1; i < sqrt(n); i++)
{
if (n % i == 0)
{
// For n : (1, n) will always be pair of divisor
// acc to def., we must ignore adding n itself as divisor
// when calculating for abundant number
if(i == 1)
sum = sum + i;
// Example For 100 (10,10) will be one of the pair
// But, we should add value 10 to the sum just once
else if(i == n/i)
sum = sum + i;
// add both pairs as divisors
// For any divisor i, n/i will also be a divisor
else
sum = sum + i + n/i;
}
}
return sum;
}
int main ()
{
int num1 = 30, num2 = 140;
int sum1 = getDivisorsSum(num1);
int sum2 = getDivisorsSum(num2);
if(sum1/num1 == sum2/num2)
cout << num1 << " & " << num2 << " are friendly pairs";
else
cout << num1 << " & " << num2 << " are not friendly pairs";
}
// Time complexity: O(√N)
// Space complexity: O(1)
Output
30 & 140 are friendly pairs
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python

Login/Signup to comment