Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check Amicable Number

Posted in Java Tutorial
Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 58 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Java Program to Check Amicable Numbers

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, exploring number properties is a fascinating journey. One interesting concept is that of amicable numbers.

Amicable numbers are two numbers that share a unique relationship: the sum of the proper divisors of each number is equal to the other number.

In simpler terms, the sum of the divisors of one number is the other number itself.

In this tutorial, we'll delve into a Java program designed to check whether two given numbers are amicable or not.

The program will identify and compare the sum of proper divisors to determine if the numbers form an amicable pair.

πŸ“„ Example

Let's take a look at the Java code that accomplishes this task.

AmicableNumbers.java
Copied
Copy To Clipboard
public class AmicableNumbers {

// Function to calculate the sum of proper divisors of a number
static int sumOfDivisors(int num) {
  int sum = 1; // Start with 1 as every number is divisible by 1

  for (int i = 2; i * i <= num; ++i) {
    if (num % i == 0) {
      sum += i;
      if (i != num / i) {
        sum += num / i;
      }
    }
  }

  return sum;
}

// Function to check if two numbers are amicable
static boolean areAmicable(int num1, int num2) {
  return sumOfDivisors(num1) == num2 && sumOfDivisors(num2) == num1;
}

// Driver program
public static void main(String[] args) {
  // Replace these values with your desired numbers
  int number1 = 220;
  int number2 = 284;

  // Check if the numbers are amicable
  if (areAmicable(number1, number2)) {
    System.out.println(number1 + " and " + number2 + " are amicable numbers.");
  } else {
    System.out.println(number1 + " and " + number2 + " are not amicable numbers.");
  }
}
}

πŸ’» Testing the Program

To test the program with different numbers, replace the values of number1 and number2 in the main method.

Output
220 and 284 are amicable numbers.

Compile and run the program to check if the numbers form an amicable pair.

🧠 How the Program Works

  1. The program defines a class AmicableNumbers containing a static method sumOfDivisors to calculate the sum of proper divisors for a given number.
  2. Another static method, areAmicable, checks whether two numbers are amicable by comparing the sums of their proper divisors.
  3. The main method tests the amicability of two numbers and prints the result.

🧐 Understanding the Concept of Amicable Numbers

Amicable numbers exhibit a unique relationship where the sum of the proper divisors of each number is equal to the other number.

For example, the pair (220, 284) is amicable because the sum of divisors of 220 is 284, and the sum of divisors of 284 is 220.

🎒 Optimizing the Program

While the provided program is effective, you may explore optimizations such as caching the results of the sum of divisors to enhance performance for repeated calculations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (Java Program to Check Amicable Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy