Menu
Topics Index
...
`

Find if the given numbers are Amicable


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find if the given numbers are Amicable. For two numbers to be amicable, the sum of the proper factors of first number is equal to the second number and sum of the proper factors of second number is equal to the first number.

For e.g., the factors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 and there sum is 284. Where as for 284 the factors are 1, 2, 4, 71, 142 and there is 220. So the numbers 220 and 284 are amicable. Note that although the number is factor of itself, we should not consider it while summing up.

Input (int, int) Output (boolean)
220, 284 true
150, 174 false
1184, 1210 true
512, 438 false



class AreNumbersAmicable
{
    public static void main(String s[])
    {
        System.out.println("The given numbers are Amicable : " + findAmicableNumbers(220, 284));

    }


    public static boolean findAmicableNumbers(int firstNumber, int secondNumber) {
    }
    

}
Topic: for Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

here we need to find the sum of factors of given 2 numbers if the sum1==num1 and sum2==num2 than the both are amicable.

* in the provided space write the method with the name getSum()
*sum variable initialize with 0
* in that method start the loop for(i=1;i<num;i++)
if the i is factor to the num than add to the Sum
After coming out of the loop return the sum;

* now in the findAmicableNumbers() invoke the getSum() with num1 and num2 and store that values in sum1 and sum2 variables after invoking the method we will have the sum so now check if(sum1==num2&&sum2==num1) than return true else return false;

Note:here we do not add the num it self while adding to the sum thats y we write loop as <num

Posted by Uday Kumar    2015-02-13 05:37:34


factors(input):
here first write function a it take a  input as number and return a sum of its factors
int factors(input):
{
ex= input=24
sum of factors(except 24)=1+2+3+4+6+8+12=36
and return the sum value
}

next in method  findAmicableNumbers(first number,second number)
write condition like
if((factors(first number)==secondnumber) or (factors(secondnumber)==firstnumber))
   above condition both are  AmicableNumbers then  return true;
otherwise
  both are not  AmicableNumbers then  return false;

Posted by Goutham Singarapu    2015-02-13 05:45:05


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Goutham Singarapu,  for 'Second Correct Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-16 07:50:50


© meritcampus 2019

All Rights Reserved.

Open In App