Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check Abundant Number

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

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, understanding and identifying special types of numbers is a fascinating exploration. One such category is abundant numbers.

An abundant number, also known as an excessive number, is a positive integer that is smaller than the sum of its proper divisors (excluding itself).

In this tutorial, we will delve into a Java program designed to check whether a given number is an abundant number or not.

The program will employ logic to calculate the sum of the proper divisors of a number and determine if it exceeds the number itself.

πŸ“„ Example

Let's take a look at the Java code that achieves this functionality.

AbundantNumberChecker.java
Copied
Copy To Clipboard
public class AbundantNumberChecker {

// Function to check if a number is abundant
static boolean isAbundant(int num) {
  int sum = 1; // Start with 1 as every number is divisible by 1

  // Iterate from 2 to the square root of the number
  for (int i = 2; i * i <= num; ++i) {
    if (num % i == 0) {
      sum += i;
      if (i != num / i) {
        sum += num / i;
      }
    }
  }

  // If the sum of divisors exceeds the number, it is abundant
  return sum > num;
}

// Driver program
public static void main(String[] args) {
  // Replace this value with your desired number
  int number = 12;

  // Check if the number is abundant
  if (isAbundant(number)) {
    System.out.println(number + " is an abundant number.");
  } else {
    System.out.println(number + " is not an abundant number.");
  }
}
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the main method.

Output
12 is an abundant number.

Compile and run the program to check if the number is an abundant number.

🧠 How the Program Works

  1. The program defines a class AbundantNumberChecker containing a static method isAbundant that takes a number as input and returns true if the number is abundant and false otherwise.
  2. Inside the method, it iterates through numbers from 2 to the square root of the given number to find its divisors.
  3. For each divisor found, it adds it to the sum, including the corresponding divisor on the other side of the square root.
  4. Finally, it compares the sum of divisors with the original number to determine if it is abundant.

πŸ“ Between the Given Range

Let's explore the Java code that checks for abundant numbers within the specified range.

AbundantNumbersInRange.java
Copied
Copy To Clipboard
public class AbundantNumbersInRange {

  // Function to check if a number is abundant
  static boolean isAbundant(int num) {
    int sumOfDivisors = 1; // Initialize sum with 1 as 1 is a proper divisor for all numbers

    for (int i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0) {
        sumOfDivisors += i;

        // If the divisors are the same, only count one of them
        if (i != num / i) {
          sumOfDivisors += num / i;
        }
      }
    }

    return sumOfDivisors > num;
  }

  // Driver program
  public static void main(String[] args) {
    // Define the range
    int startRange = 1;
    int endRange = 50;

    System.out.println("Abundant numbers between " + startRange + " and " + endRange + ":");

    for (int i = startRange; i <= endRange; i++) {
      if (isAbundant(i)) {
        System.out.print(i + " ");
      }
    }
  }
}

πŸ’» Testing the Program

The program is set to check for abundant numbers in the range from 1 to 50. You can adjust the range by modifying the values of startRange and endRange in the code.

Output
Abundant numbers between 1 and 50:
12 18 20 24 30 36 40 42 48

Run the program to see the abundant numbers within the specified range.

🧠 How the Program Works

  1. The program defines a class AbundantNumbersInRange containing a static method isAbundant to check if a given number is abundant.
  2. Inside the isAbundant method, it calculates the sum of proper divisors of the number.
  3. The main method defines the range (from 1 to 50) and iterates through each number, checking if it's abundant. If so, it prints the number.

🧐 Understanding the Concept of Abundant Number

Before diving into the code, let's take a moment to understand abundant numbers. An abundant number is a positive integer for which the sum of its proper divisors (excluding itself) is greater than the number itself.

For example, the number 12 is abundant because its divisors (excluding 12) are 1, 2, 3, 4, 6, and the sum of these divisors is 16, which is greater than 12.

🎒 Optimizing the Program

While the provided program is effective, there are opportunities for optimization, such as caching previously calculated divisor sums to avoid redundant computations.

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
10 months ago

If you have any doubts regarding this article (Java Program to Check Abundant 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