Write a program in java which will display whether two numbers entered by the user are amicable pair or not using the following method prototype : void amicable ( int m, int n)
Hint: Amicable pair means “Sum of factors of first number excluding the number itself, is same with second number and sum of factors of second number excluding the number itself, is same with first number.
public class Main {
public static void amicable(int m, int n) {
int totalM = 0;
int totalN = 0;
for (int i = 1; i <= m / 2; i++) {
if (m % i == 0) {
totalM += i;
}
}
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
totalN += i;
}
}
System.out.println(totalM == n && totalN == m && m > 0 && n > 0 ? "Yes" : "No");
}
public static void main(String[] args) {
amicable(5020, 5564);
}
}
Comments
Leave a comment