Program To Print Amicable Pair Number JAVA


Write a Program to print whether the inputted numbers is Amicable Pair or not.




The amicable numbers are two different numbers (a pair of numbers) so related that the sum of the factors (excluding the number itself) of one of the number is equal to the other.
Example : 220 & 284
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

import java.util.*;
class amicable
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number");
        int a = sc.nextInt();
        System.out.println("Enter the second number");
        int b = sc.nextInt();
        int sum_a = 0, sum_b = 0;
        for(int i = 1;i < a;i++)
        {
            if(a % i == 0)
            {
                sum_a = sum_a + i;
            }
        }
        for(int i = 1;i < b;i++)
        {
            if(b % i == 0)
            {
                sum_b = sum_b + i;
            }
        }
        if(sum_a==b && sum_b==a)
        {
         System.out.println("It is amicable pair");   
        }
        else
        {
         System.out.println("It is not amicable pair");
        }
    }
}

     
                     

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com