Amicable Numbers Program in Java
Two different numbers are said to be so Amicable Numbers if each sum of divisors is equal to the other number.
Amicable Numbers are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368) etc.
Example– 220 and 284 are Amicable Numbers.
Divisors of 220 = 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
1+2+4+5+10+11+20+22+44+55+110=284
Divisors of 284 = 1, 2, 7, 71, 142
1+2+7+71+142=220
import java.util.Scanner;
public class AmicableNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 1st number= ");
int a = sc.nextInt();
System.out.print("Enter 2nd number: ");
int b = sc.nextInt();
int sumA = 0, sumB = 0;
for (int i = 1; i < a; i++)
{
if (a % i == 0)
{
sumA += i;
}
}
for (int i = 1; i < b; i++)
{
if (b % i == 0)
{
sumB += i;
}
}
if (sumA == b && sumB == a)
{
System.out.println("The numbers are Amicable Number.");
}
else
{
System.out.println("The numbers are not Amicable Number");
}
}
}
Output:
Enter 1st number= 284 Enter 2nd number: 220 The numbers are Amicable Number.