Answer to Question #257517 in Java | JSP | JSF for Nikhil Sahoo

Question #257517

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.


1
Expert's answer
2021-10-30T22:39:28-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS