In the previous article, we have discussed Java program to Check Narcissistic Decimal Number

## Program to Check Two Numbers are Amicable Numbers

In this article we are going to understand what Amicable number is and how we can check whether a number is Amicable or not in Java with examples.

**Amicable numbers** are a set of two numbers whose sum of all divisors are equal to the other number.

Example : (220 and 284): Divisors sum of 220 = 1+2+4+5+10+11+20+22+44+55+110 = 284 Divisors sum of 284 = 1+2+4+71+142 = 220 220 and 284 are Amicable Numbers

In the above examples the numbers 220 and 284 are Amicable numbers as their divisors add up to the other number.

Let’s see different ways to check Amicable number.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

**Approach :**

- Enter/declare two numbers and store it .
- We add all the divisors of both the numbers and compare them.
- If both the sums are equivalent to the other entered number, then the numbers are said to be Amicable numbers.

### Method-1: Java Program to Check Two Numbers are Amicable Numbers By User Input Value

import java.util.Scanner; public class AmicableNumber{ public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.println("Enter two numbers : "); int num1 = scan.nextInt(), num2 = scan.nextInt(); int sum1 = divisorSum(num1), sum2 = divisorSum(num2); if((sum1==num2)&&(sum2==num1)) { System.out.println(num1+" and "+num2+" are Amicable numbers"); } else { System.out.println(num1+" and "+num2+" are Not Amicable number"); } } // Function to find the sum of divisors static int divisorSum(int num) { int sum = 0; // Checks all the numbers from 1 to num that divides num and leaves remainder 0 for(int i =1;i<num;i++) if(num%i==0) sum+=i; return sum; } }

Output: Case-1 Enter two numbers : 220 284 220 and 284 are Amicable Numbers Case-2 Enter two numbers : 553 768 553 and 768 are Not Amicable number

### Methdo-2: Java Program to Check Two Numbers are Amicable Numbers By User Defined Method

import java.util.Scanner; public class AmicableNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.println("Enter two numbers : "); int num1 = scan.nextInt(), num2 = scan.nextInt(); int sum1 = divisorSum(num1), sum2 = divisorSum(num2); //calling checkNumber() method checkNumber(num1,sum1,num2,sum2); } //user defined method //checkNumber() method to check Amicable number public static void checkNumber(int num1, int sum1, int num2, int sum2) { if((sum1==num2)&&(sum2==num1)) { System.out.println(num1+" and "+num2+" are Amicable numbers"); } else { System.out.println(num1+" and "+num2+" are Not Amicable number"); } } // Function to find the sum of divisors static int divisorSum(int num) { int sum = 0; // Checks all the numbers from 1 to num that divides num and leaves remainder 0 for(int i =1;i<num;i++) if(num%i==0) sum+=i; return sum; } }

Output: Case-1 Enter two numbers : 220 284 220 and 284 are Amicable Numbers Case-2 Enter two numbers : 553 768 553 and 768 are Not Amicable number

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

**Related Java Programs:**