Amicable Pair Number in Java
In this section, we will learn what is an amicable number and also create Java programs to check if the given number is an amicable number or not. The amicable number program frequently asked in Java coding tests and academics.
The amicable numbers are two different numbers (a pair of numbers) so related that the sum of the proper divisors (excluding the number itself) of one of the numbers is equal to the other. A proper divisor of a number is a divisor other than the number itself. It is also known as friendly numbers.
The sum of the divisor of the first number = The sum of the divisor of the second number
The sum of the divisor of the second number = The sum of the divisor of the first number
The smallest pair of amicable numbers is (220, 284). Because:
The divisor of 220 are: 1,2,4,5,10,11,20,22,44,55,and 110
The sum of divisor of 220 is = 284
The divisor of 284 are: 1,2,4,71,142
The sum of divisor of 284 is = 220
We observe that the sum of the divisors of the first number is equal to the second number, and the sum of divisor of the second number is equal to the first number. Hence, the given pair (220, 284) is an amicable pair.
Amicable Numbers Example
Some other amicable pair numbers are (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).
Steps to Find Amicable Number
Let's implement the above steps in a Java program.
Amicable Number Java Program
The logic is very easy. First, we will compare the sum of both numbers and compare the sum for one number with the second number.
Enter the first number: 5020 Enter the second number: 5564 5020, 5564 are amicable numbers.
Enter the first number: 12 Enter the second number: 45 12, 45 are not amicable numbers.
Let's create another Java program by using a different logic.
Enter the first number: 220 Enter the second number: 284 The pair of numbers are amicable.
Enter the first number: 60 Enter the second number: 84 The pair of numbers are not amicable.