Program to check Amicable Number in java

/*
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself.
*/

import java.util.*;
public class AmicableNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value for first number");
int a = sc.nextInt();
System.out.println("Enter value for second number: ");
int b = sc.nextInt();
int sum1 = 0, sum2 = 0;
System.out.println("Factors of " + a + " are=");
for (int i = 1; i < a; i++) {
if (a % i == 0) {
System.out.print(i + " ");
sum1 = sum1 + i;
}
}
System.out.println("  sum of factor a==" + sum1);
System.out.println();
System.out.println("Factors of " + b + " are=");
for (int i = 1; i < b; i++) {
if (b % i == 0) {
System.out.print(i + " ");

sum2 = sum2 + i;
}
}
System.out.print("  sum of factor b==" + sum2);
System.out.println();
System.out.println();
System.out.println("sum of first number == another number then no is amicable");
System.out.println();
System.out.println();
if (sum1 == b) {
System.out.println("Numbers are amicable");
} else {
System.out.println("Numbers are not amicable");
}
}
}

Output:


Comments