Write a program to accept two numbers n1 and n2 in the main method. With the help of a function void amicable(int,int) display the message"amicable" if they are amicable otherwise display the message "not amicable" if they are not amicable.
[Amicable numbers are two nos if each is equal to the sum of the proper divisors of the other nos .]
Example -- 220 and 284 are amicable numbers.
Answers
import java.io.*;
class GFG
{
static int divSum(int n)
{
int result = 0;
for (int i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + 1);
}
static boolean areAmicable(int x, int y)
{
if (divSum(x) != y)
return false;
return (divSum(y) == x);
}
public static void main (String[] args)
{
int x = 220, y = 284;
if (areAmicable(x, y))
System.out.println( "Yes");
else
System.out.println("No");
}
}