Computer Science, asked by vikrambalaji215, 1 year ago

write a java program for amicable numbers​

Answers

Answered by Anonymous
2

Amicable numbers come in a pair. So whenever we write an Amicable number we write them in a pair. The smallest Amicable Number pair is (220, 284).

Let’s understand what is an amicable number. Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.

For 220 the proper divisors are 1, 2 ,4, 5, 10, 11, 20, 22, 44, 55 and 110

And For 284 the proper divisors are 1, 2, 4, 71 and142 .

let’s make a sum of 220 proper divisors : 1+2 + 4 + 5 + 10+ 11 + 20 + 22 + 44 + 55 = 284 .

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).

I hope you have learned about Amicable Numbers and you also learned that how to write a java program to check the numbers are amicable or not. Keep Learning best wishes to you.

And the sum of 284 proper divisors : 1 + 2 + 4 + 71 +142 = 22

Attachments:
Answered by sushiladevi4418
0

Answer:

Java program for amicable numbers.

Explanation:

 import java.util.Scanner;

 

public class AmicableNumber {

 

public static void main(String[] args) {

 

 

 int firstNumber, secodNumber;

 Scanner scanner = new Scanner(System.in);

 System.out.println("Enter two Numbers: ");

 firstNumber = scanner.nextInt();

 secodNumber = scanner.nextInt();

 

 boolean flag = checkAmicableOrNot(firstNumber, secodNumber);

 

 if (flag) {

  System.out.println("The numbers are amicable");

 } else {

  System.out.println("The numbers are not amicable");

 }

}

 

static boolean checkAmicableOrNot(int firstNumber, int secondNumber) {

 int s = 0, i;

 

 for (i = 1; i < firstNumber; i++) {

  if (firstNumber % i == 0) {

   s = s + i;

  }

 }

 if (s == secondNumber) {

  s = 0;

  for (i = 1; i < secondNumber; i++) {

   if (secondNumber % i == 0) {

    s = s + i;

   }

  }

  if (s == firstNumber)

   return true;

  else

   return false;

 }

 

 return false;

 

}

 

}

Similar questions