Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Abundant Number or Not in Java

Last Updated on May 15, 2023 by Prepbytes

This article discusses a program that determines if a given number is abundant or not. A number is said to be plentiful if the total of its appropriate divisors exceeds the number itself.

What is an Abundant Number in Java?

If the sum of all its proper divisors, denoted by sum(n), is greater than the number’s value, the number is said to be abundant. The difference between these two values is referred to as abundance. If the following condition is met, the number is said to be an abundant number:

  • sum(n)> n
  • abundance = sum(n) – n
  • sum(n): aliquot sum – the sum of all n’s proper divisors.

Check whether the Number is an Abundant Number or Not in Java

Given an integer input, the goal is to determine whether the sum of all the number’s factors, excluding the number itself, is greater than the number. To be an abundant number, any number must satisfy the following conditions:

  1. Except for itself, the sum of its factors must be greater than the number.
  2. As a result, we will first find all the factors of the number that do not include the number itself within the range 1 to number/2.
  3. Meanwhile, we’ll keep adding up the factors and compare them to the final figure.
  4. If the number itself is included as a factor, the sum must be divided by 2 before comparing.

We will look at two approaches to resolving this problem.

Method 1: Using Java’s Range until Number

  1. Begin the program.
  2. Declare and initialize two integer variables, ‘n’, and sum with values 12 and 0.
  3. Use a for loop with variable ‘i’ that starts at 1 and ends at ‘n-1’.
  4. Check to see if ‘n’ is divisible by ‘i’, that is, n% i == 0’.
  5. If ‘n’ is divisible by ‘i’, add ‘i’ to sum, i.e.’sum = sum + i’.
  6. Determine whether the sum is greater than ‘n’.
  7. If the sum is greater than the number n, print "n is an Abundant Number."
  8. Subtract ‘n’ from sum’ to find the abundance of the number.
  9. "The Abundance is:" followed by the calculated abundance value.
  10. If Sum is less than or equal to ‘n,’ display "n is not an Abundant Number."
  11. Finish the program.

Code Implementation

public class Main {
public static void main (String[]args) {
int n = 12, sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum > n) {
System.out.println(n + " is an Abundant Number");
System.out.println("The Abundance is: " + (sum - n));
}
else
System.out.println(n + " is not an Abundant Number");
}
}

Output:

12 is an Abundant Number
The Abundance is: 6

Method 2: Using Java’s Range until Sqrt

  1. Begin by declaring and initializing the variable n to a suitable integer value.
  2. Declare the variable sum and assign it the outcome of calling the getSum function with the argument n.
  3. Check to see if the total is greater than n.
  4. If the sum exceeds n, a message indicating that n is an abundant number and the abundance value is printed.
  5. If the sum is less than n, a message indicating that n is not an abundant number is printed.
  6. Declare the function getSum with an integer parameter n.
  7. Declare and set a variable sum to 0.
  8. Iterate from 1 to the square root of n using a for loop.
  9. Check whether n is divisible by i within the loop.
  10. If n is divisible by i, find the two divisors that form the pair with i and add them to the sum.
  11. If i equals 1, then only i should be added to the sum.
  12. If i equals the square root of n, then only i should be added to the sum.
  13. The function’s sum value is returned.
  14. The program has come to an end.

Code Implementation

public class Main 
{
  public static void main(String[] args) {
    int n = 12;
    int sum = getSum(n);
    if (sum > n) {
      System.out.println(n + " is an Abundant Number");
    } else {
      System.out.println(n + " is not an Abundant Number");
    }
  }

  static int getSum(int n) {
    int sum = 0;
    for (int i = 1; i < Math.sqrt(n); i++) {
      if (n % i == 0) {
        if (i == 1)
          sum = sum + i;
        else if (i == n / i)
          sum = sum + i;
        else
          sum = sum + i + n / i;
      }
    }
    return sum;
  }
}

Output

12 is an Abundant Number

Conclusion
Finally, this article describes the idea of abundant numbers and includes two Java methods for determining if a given number is abundant or not. Method 1 used a for loop to run through the range 1 to n-1, determining if n is divisible by i and adding i to the sum. Method 2 uses a function to compute the sum of all components until the square root of n is reached. For the provided input of 12, which is an abundant number with an abundance of 6, both approaches generate the same outcome. These methods can be used for any integer input to determine whether or not it is an abundant number.

Frequently Asked Questions (FAQs)

Q1. Is 32 abundant or deficient?
Ans. 32 is not an abundant number. Its proper divisors are 1, 2, 4, 8, and 16, and their sum is 31 (less than 32). As a result, 32 is the least sum of two distinct abundant numbers that are not abundant.

Q2. What are abundant, deficient, and perfect numbers in Java?
Ans. If the sum of the factors excluding itself equals that number, it is perfect; if it exceeds that number, it is abundant; and if it falls below that number, it is deficient.

Q3. Which numbers are not abundant?
Ans. Non-abundant numbers are classified into two types: perfect numbers and deficient numbers. A perfect number is one in which the sum of its proper divisors equals the number itself. A deficient number is one in which the sum of its proper divisors is less than the number itself.

Leave a Reply

Your email address will not be published. Required fields are marked *