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
Here we will discuss how to check whether a pair of numbers is a Friendly Pair(Amicable numbers) or not using C++ programming language.
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.
Let’s see how the code works:
- 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
//Friendly Pair(Amicable number) or not
using namespace std;
// function to check Friendly pairs
void findAmicable(int first, int second)
for(int i=1; i<=first/2 ; i++)
//finding and adding divisors of first number
for(int i=1; i<=second/2 ; i++)
//finding and adding divisors of second number
//ckecking for friendly pair
if(first==sum2 && second==sum1)
cout<<"Not a Fiendly Pair";
cout<<"Enter first number : ";
cout<<"Enter second number : ";
Enter first number : 220
Enter second number : 284