Abundant number or not using Java

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

Given an integer input, the objective is to check whether the sum of all the factors of a number except the number itself is greater than the number or not. Therefore, we’ll write a code to Check Whether or Not the Number is an Abundant Number in Java.

Example
Input : 18
Output : Yes, it's an Abundant Number
abundant or not in java

Check Whether or Not the Number is an Abundant Number in Java Language

Given an integer input, the objective is to check whether or not the sum of all the factors of the number, except the number itself is greater than the number. For any number to be an Abundant Number, the sum of it’s factors except itself must be greater than the number. Therefore, we’ll first find all the factors of the number without the number itself within the range 1 to the number/2. We’ll keep summing the factors meanwhile and finally check with the actual number. If we Include the number itself as a factors then the sum must be divided by 2 before comparing. Here are some of the methods to Check Whether or Not the Number is an Abundant Number in Java Language

  • Method 1: Using Range until Number
  • Method 2: Using Range until Sqrt( Number )

We’ll discuss the above mentioned methods in detail in the upcoming sections. Do check out the blue box given below for better understanding of the above mentioned questions.

Method 1: Using Range until Number

Python Code

Run
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: 4

Method 2: Using Range until sqrt( Number )

Python Code

Run
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");
    	System.out.println ("The Abundance is: " + (sum - n));
      }
    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) 
        { 
            // For n : (1, n) will always be pair of divisor 
            // acc to def., we must ignore adding n itself as divisor 
            // when calculating for abundant number 
            if(i == 1) 
                sum = sum + i; 

            // Example For 100 (10,10) will be one of the pair 
            // But, we should add value 10 to the sum just once 
            else if(i == n/i) 
                sum = sum + i; 

            // add both pairs as divisors 
            // For any divisor i, n/i will also be a divisor 
            else 
                sum = sum + i + n/i; 
        } 
    } 
    return sum; 
} 
}

Output

12 is an Abundant Number
The Abundance is: 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

2 comments on “Abundant number or not using Java”


  • Soumen

    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int Num = sc.nextInt();
    int Sum = 0;
    int temp = Num;
    for (int i = 1; iNum) {
    System.out.println(“Abudant Number”);
    } else {
    System.out.println(“Not an Abudant Number”);
    }
    }

    }


  • Harsh

    //An abundant number is a number for which the sum of its proper factors is greater than the number itself.

    import java.util.*;

    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int sum = 0;
    System.out.print(“Enter a no.: “);
    int no = sc.nextInt();
    int num = no;
    for(int i=1; ino){ //if the calculated sum is greater than given number, then number is abundant
    System.out.println(no+ ” is abundant.”);
    int abu = sum – no; //Calculating the abundance factor
    System.out.println(“Its abundance is: “+ abu);
    }

    else
    System.out.println(“No is not abundant”);
    }
    }