Java: Check two numbers are Amicable numbers or not
Java Numbers: Exercise-16 with Solution
Check Amicable Numbers
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:
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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/numbers/java-number-exercise-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics