Java program to check if two numbers are Amicable or not:
In this post, we will learn how to check if two user provided numbers are amicable or not. The program will take these numbers as inputs from the user and print one message based on the inputs.
What are amicable number pair:
A pair of numbers are called amicable numbers if the sum of the proper divisors of one number is equal to the other number. The proper divisors list doesn’t include the number itself.
For example, 220 and 284 are Amicable numbers. Because,
Sum of proper divisors of 220 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284 Sum of proper divisors of 284 = 1 + 2 + 4 + 71 + 142 = 220
So, 220 and 284 are Amicable numbers.
Algorithm to find Amicable numbers:
We will use the below algorithm to find if two numbers are Amicable or not:
- Take the first number as input from the user and store it in a variable.
- Take the second number as input from the user and store it in another variable.
- Find the divisors of both numbers and find the sum of the divisors by using a loop.
- Check if the sum of divisors of one number is equal to the other number or not.
- Based on the result, print one message. If the sum of divisors of one number is equal to the other number, print that both are Amicable. Else, print that they are not Amicable.
Method 1: Java program to find Amicable numbers:
Below is the complete Java program to check for Amicable numbers:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int firstNumber, secondNumber, sumFirst, sumSecond;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
firstNumber = sc.nextInt();
System.out.println("Enter the second number: ");
secondNumber = sc.nextInt();
for (int i = 1; i < firstNumber; i++) {
if (firstNumber % i == 0)
sumFirst += i;
}
for (int i = 1; i < secondNumber; i++) {
if (secondNumber % i == 0)
sumSecond += i;
}
if (sumFirst == secondNumber && sumSecond == firstNumber) {
System.out.println("These are Amicable numbers");
} else {
System.out.println("These are not Amicable numbers");
}
}
}
In this program,
- firstNumber and secondNumber are two integer values to hold the numbers entered by the user.
- sumFirst and sumSecond are two integers to hold the sum of divisors of firstNumber and secondNumber. These are 0 on initialization.
- The Scanner object sc is used to read the user input values.
- The program asks the user to enter the first and the second numbers and stored them in the firstNumber and secondNumber variables.
- The first for loop runs from 1 to firstNumber - 1. For each value of i, it checks if it is a divisor of the number or not. If yes, it adds that value to sumFirst. Similarly, it finds the divisors of secondNumber and adds those to sumSecond.
- The last if-else statement checks if the sum of divisors of the first number is equal to the second number and the sum of divisors of the second number is equal to the first number or not. If yes, it prints that these are Amicable numbers. Else, it prints that these are not Amicable numbers.
If you run this program, it will print output as like below:
Enter the first number:
120
Enter the second number:
220
These are not Amicable numbers
Enter the first number:
220
Enter the second number:
284
These are Amicable numbers
Method 2: How to improve this program:
We can improve the above program. The for loops are running from 1 to number - 1. But we can change these loops to run from 1 to number/2 because no numbers greater than number/2 can be a divisor of a number*.
So, it will be:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int firstNumber, secondNumber, sumFirst, sumSecond;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
firstNumber = sc.nextInt();
System.out.println("Enter the second number: ");
secondNumber = sc.nextInt();
for (int i = 1; i <= firstNumber/2; i++) {
if (firstNumber % i == 0)
sumFirst += i;
}
for (int i = 1; i <= secondNumber/2; i++) {
if (secondNumber % i == 0)
sumSecond += i;
}
if (sumFirst == secondNumber && sumSecond == firstNumber) {
System.out.println("These are Amicable numbers");
} else {
System.out.println("These are not Amicable numbers");
}
}
}
If you run this program, it will give similar result.
The advantage of this that it is faster than the previous program. The loops are running half the time than the previous one. If we have a huge number, it will reduce a significant amount of time.
Method 3: By comparing the sum:
Once both loops are completed, for Amicable numbers, the sum for the first number is equal to the second number and the sum for the second number is equal to the first number. So, if we add the numbers with their divisors, both should be equal for Amicable numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int firstNumber, secondNumber, sumFirst = 0, sumSecond = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
firstNumber = sc.nextInt();
System.out.println("Enter the second number: ");
secondNumber = sc.nextInt();
sumFirst = firstNumber;
sumSecond = secondNumber;
for (int i = 1; i <= firstNumber/2; i++) {
if (firstNumber % i == 0)
sumFirst += i;
}
for (int i = 1; i <= secondNumber/2; i++) {
if (secondNumber % i == 0)
sumSecond += i;
}
if (sumFirst == sumSecond) {
System.out.println("These are Amicable numbers");
} else {
System.out.println("These are not Amicable numbers");
}
}
}
In this example, we have made changes to the following lines:
sumFirst = firstNumber;
sumSecond = secondNumber;
and
if (sumFirst == sumSecond) {
....
i.e. the sum variables are initialized as the numbers. So, if we add the divisors, both will be equal.
If you run this program, it will give similar results.
You might also like:
- Java program to find the sum of 0, 1, 1, 2, 3, 5, 8.. or Fibonacci series
- How to find the velocity in Java using user-give values
- Java program to convert a decimal to binary format for a float or double value
- How to add a character to the start and end of a string in Java
- Java program to add characters to the middle of a string
- How to remove the first element of an ArrayList in Java
- Java program to check if a number is Automorphic or not