Amicable number in java - what is amicable number

 

Amicable number in java

What is the amicable number?

A pair of number so different but the sum of its proper divisor is equal to each other is called Amicable number.

Examples are

220, 284

2620, 2924
5020, 5564
6232, 6368

Let’s elaborate it 2620 and 2924

2620 proper divisors: 

1 2 4 5 10 20 131 262 524 655 1310

=2924

 

2924 proper divisors:

1 2 4 17 34 43 68 86 172 731 1462

=2620

 

Here 2620 divisor sum is 284 and 284 divisor sum is 2924 which equal to each other.

 

In the following program, we have taken two number1=12620 and number2=2924 and calculate its summation of numbers divisor.

 

Amicable number in java

 public class AmicableNumber

{

    public static void main(String args[])

    {

        int sum1,sum2,i;

        int number1 = 2620;

        int number2 = 2924; // pair of two number

        sum1 = sum2 = 0;

        // First number sum

        System.out.print(number1 + "=");

        for(i=1; i<number1 ;i++)

        {

            if(number1 % i == 0)

            {

                System.out.print(i + " ");

                sum1 = sum1 + i;

            }

        }

        System.out.println("=" + sum1);

        // Second number sum

        System.out.print(number2 + "=");

        for(i=1; i<number2 ;i++)

        {

            if(number2 % i == 0)

            {

                System.out.print(i + " ");

                sum2 = sum2 + i;

            }

        }

        System.out.println("=" + sum2);

        // cheching of first number sum to first number

        // second to first

        if(sum1 == number2 && sum2 == number1)

        {

            System.out.println("\nAmicable Number");

        }

        else

        {

            System.out.println("\nNot amicable Number");

        }

    }

}

Output

2620=1 2 4 5 10 20 131 262 524 655 1310 = 2924

2924=1 2 4 17 34 43 68 86 172 731 1462 = 2620

 Amicable Number

Java program



SHARE THIS
Previous Post
Next Post