w3resource

Java: Check two numbers are Amicable numbers or not

Java Numbers: Exercise-16 with Solution

Write a Java program to check two numbers are Amicable numbers or not.

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.
The first ten amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).

Test Data
Input the first number: 220
Input the second number: 284

Pictorial Presentation:

Java: Check two numbers are Amicable numbers or not.

Sample Solution:

Java Code:

import java.util.Scanner;

public class Example16
    {
      public static void main(String args[]) {
            Scanner in = new Scanner(System.in);
            System.out.print("Input the first number: ");
            int num1 = in.nextInt();
            System.out.print("Input the second number: ");
            int num2 = in.nextInt();
            int sum_num1 = 0, sum_num2 = 0;
            for (int i = 1; i <= num1; i++) {
                if (num1 % i == 0)
                    sum_num1 += i;
            }
            for (int i = 1; i <= num2; i++) {
                if (num2 % i == 0)
                    sum_num2 += i;
            }
            if (sum_num1 == sum_num2)
                System.out.println("These numbers are amicable.");
            else
                System.out.println("These numbers are not amicable.");
				System.out.println("\n");			
    }
}

Sample Output:

Input the first number: 220                                                                                   
Input the second number: 284                                                                                  
These numbers are amicable.

Flowchart:

Flowchart: Check two numbers are Amicable numbers or not

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to check whether a number is a Duck Number or not.
Next: Write a Java program to check if a given number is circular prime or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

IsPowerOfTwo

Checks if a value is positive power of two.

To understand how it works let's assume we made a call IsPowerOfTwo(4).

As value is greater than 0, so right side of the && operator will be evaluated.

The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.

The result of (value & value) is value. 100 & 100 => 100.

This will value the expression to true as value is equal to value.

public static boolean isPowerOfTwo(final int value) {
    return value > 0 && ((value & (~value + 1)) == value);
}

Ref: https://bit.ly/3sA5d4I

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook