C++ question Two distinct, integer numbers, x and y are amicable numbers if the
ID: 3777798 • Letter: C
Question
C++ question
Two distinct, integer numbers, x and y are amicable numbers if the sum of x’s proper divisors equals y and the sum of y’s proper divisors equals x. From Wikipedia “The smallest pair of amicable numbers is (220, 284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.” Write a function, amicablePair, that returns true if its two integer parameters are amicable and false otherwise. The function’s protoype is bool amicablePair(int a, int b);
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{ int x, y;
int xDivSum =0 , yDivSum =0;
cout<<"Please enter the first number"<<endl;
cin>>x;
cout<<"Please enter second number"<<end;
cin>>y;
//let us find the divisor of each number and fetch their sum
for(int i=2;i<=x/2;i++)
{
if(x%i ==0)
{xDivSum = xDivSum+i;}
}
for(int i=2;i<=y/2;i++)
{
if(y%2==0)
{yDivSum = yDivSum+i;
}
if(xDivSum == y && yDivSum == x)
{cout<<"Entered number forms a amicable pair"<<endl;
}
else
{cout<<"Entered number are not amicable pair"<<endl;}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.