











C++ Program to check whether two numbers are Friendly Pair(Amicable number) or not
Program to check whether two numbers are Friendly Pair(Amicable number) or not
Amicable numbers are two different numbers related in a way such that the sum of the proper divisors of each is equal to the other number. A proper divisor is a positive factor of that number other than the number itself ,for Example proper divisors of 6 are 1, 2, and 3.
The smallest pair of amicable numbers is (220, 284). They are amicable because the sum of proper divisors of 220 (1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110) is 284; and the sum of proper divisors of 284 (1, 2, 4, 71 and 142) is 220.




Algorithm:-
- User gives two inputs(numbers to check).
- Inputs are stored in int type variables say first & second.
- A function findAmicable() is called with first and second as parameters.
- Divisors of first are found, added and stored in an int type variable say sum1.
- Divisors of second are found, added and stored in another variable sum2.
- If first is equal to sum2 and second is equal to sum1 then they are Amicable numbers
- Otherwise they are not Amicable numbers
C++ Code:-
//C++ Program
//Friendly Pair(Amicable number) or not
#include<iostream>
using namespace std;
// function to check Friendly pairs
void findAmicable(int first, int second)
{
int sum1=0,sum2=0;
for(int i=1; i<=first/2 ; i++)
{
//finding and adding divisors of first number
if(first%i==0)
sum1=sum1+i;
}
for(int i=1; i<=second/2 ; i++)
{
//finding and adding divisors of second number
if(second%i==0)
sum2=sum2+i;
}
//ckecking for friendly pair
if(first==sum2 && second==sum1)
cout<<“Fiendly Pair(“<<first<<“,”<<second<<“)”;
else
cout<<“Not a Fiendly Pair”;
}
//main program
int main()
{
int first,second;
cout<<“Enter first number : “;
//user input
cin>>first;
cout<<“Enter second number : “;
//user input
cin>>second;
//calling function
findAmicable(first,second);
return 0;
}
Output
Enter first number : 220
Enter second number : 284
Fiendly Pair(220,284)
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java


Login/Signup to comment