Q. Write an algorithm and program to check Abundant Number?

Program to check Abundant Number : This section focuses on Abundant Number algorithm and program. This program should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Abundant Number : An abundant number is a number for which the sum of its proper divisors is greater than the number itself.
Given an number n and our task to find sum of all the proper divisors of the number and check if sum of all the proper divisors is greater than the original number or not.

For Example :-

Input : n = 12
Output : Yes, The number is Abundant Number
Explanation : As we can see that input number is 12. The proper divisors of 12 is 1,2,3,4,6 and the sum of proper divisors is 1 + 2 + 3 + 4 + 6 = 16. The Abundance is 16 - 12 = 4.So, Yes, The number is Abundant Number". Now let us understand what logic we need to use in this :
1. Firstly, find the divisors of the number.
2. Calculate the sum of divisor.
3. Check if this sum is greater than original number or not.

Algorithm To Check Abundant Number

\\Algorithm To Check Abundant Number
START
Step 1 : Initilize the sum with 0 
Step 2 : Loop begins from i = 1 to i <= (Math.Sqrt(n)) :
				if n % i equals 0
					if n/i equals i
						sum equals sum + i
					else
						sum equals sum + i; 
                		sum equals sum + (n / i); 
           
            
Step 3 : sum equals sum - n
Step 4 : check if sum greater than original number n		
Stop


Program To Check Abundant Number

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Check Abundant Number
    #include <stdio.h> 
      
    int main()
    {
        int num=18, sum = 0; 
      
        // Note that this loop runs
        for (int i=1; i<=num/2; i++) 
        { 
            if (num%i==0) 
            { 
                // If divisors are equal,take only one 
                // of them 
                if (num/i == i) 
                    sum = sum + i; 
      
                else // Otherwise take both 
                { 
                    sum = sum + i; 
                    sum = sum + (num / i); 
                } 
            } 
        }
        // calculate sum of all proper divisors only 
        sum = sum - num; 
        if(sum>num)
        {
            printf("Yes, The number is Abundant Number");
        }
        else
        {
            printf("No, The number is not Abundant Number");
        }
    
        return 0;
    }
    
    //C++ Program To Check Abundant Number
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num=18, sum = 0; 
      
        // Note that this loop runs till square root 
        // of num 
        for (int i=1; i<=num/2; i++) 
        { 
            if (num%i==0) 
            { 
                // If divisors are equal,take only one 
                // of them 
                if (num/i == i) 
                    sum = sum + i; 
      
                else // Otherwise take both 
                { 
                    sum = sum + i; 
                    sum = sum + (num / i); 
                } 
            } 
        }
        // calculate sum of all proper divisors only 
        sum = sum - num; 
        if(sum>num)
        {
            cout<<"Yes, The number is Abundant Number";
        }
        else
        {
            cout<<"No, The number is not Abundant Number";
        }
    
        return 0;
    }
    
    //Java Program To Check Abundant Number
    
    public class LFC
    {
    	public static void main(String[] args) {
    	    int num=18, sum = 0; 
      
            // Note that this loop runs till square root 
            // of num 
            for (int i=1; i<=num/2; i++) 
            { 
                if (num%i==0) 
                { 
                    // If divisors are equal,take only one 
                    // of them 
                    if (num/i == i) 
                        sum = sum + i; 
          
                    else // Otherwise take both 
                    { 
                        sum = sum + i; 
                        sum = sum + (num / i); 
                    } 
                } 
            }
            // calculate sum of all proper divisors only 
            sum = sum - num; 
            if(sum>num)
            {
                System.out.println("Yes, The number is Abundant Number");
            }
            else
            {
                System.out.println("No, The number is not Abundant Number");
            }
    	}
    }
    
    
    #Python Program To Check Abundant Number
    
    import math
    num=18
    sum = 0 
      
    # Note that this loop runs till square root 
    # of num
    i=1
    while i <= (math.sqrt(num)) : 
        if num%i == 0 : 
            # If divisors are equal,take only one 
            # of them 
            if num/i == i : 
                sum = sum + i
     
            else : # Otherwise take both
                sum = sum + i
                sum = sum + (num / i)
        i = i + 1
    # calculate sum of all proper divisors only 
    sum = sum - num; 
    if sum>num:
        print("Yes, The number is Abundant Number")
    else:
        print("No, The number is not Abundant Number")
    
    //C# Program To Check Abundant Number
    
    using System;
    class LFC {
      static void Main() {
         
         	    int num=18, sum = 0; 
      
                // Note that this loop runs till square root 
                // of num 
                for (int i=1; i<=num/2; i++) 
                { 
                    if (num%i==0) 
                    { 
                        // If divisors are equal,take only one 
                        // of them 
                        if (num/i == i) 
                            sum = sum + i; 
              
                        else // Otherwise take both 
                        { 
                            sum = sum + i; 
                            sum = sum + (num / i); 
                        } 
                    } 
                }
                // calculate sum of all proper divisors only 
                sum = sum - num; 
                if(sum>num)
                {
                    Console.WriteLine("Yes, The number is Abundant Number");
                }
                else
                {
                    Console.WriteLine("No, The number is not Abundant Number");
                }
      }
    }
    
    <?php
    //PHP Program To Check Abundant Number
    
    $num=18;
    $sum=0;
    // Note that this loop runs 
    // till square root of $num 
    for ($i = 1; $i <= sqrt($num); $i++) 
    { 
        if ($num % $i == 0) 
        { 
            // If divisors are equal,take  
            // only one of them 
            if ($num / $i == $i) 
                $sum = $sum + $i; 
      
            // Otherwise take both 
            else 
            { 
                $sum = $sum + $i; 
                $sum = $sum + ($num / $i); 
            } 
        } 
    } 
    $sum = $sum - $num; 
    
    if ($sum > $num)
    {
        echo("Yes, The number is Abundant Number");
    }
    else
    {
        echo("No, The number is not Abundant Number");
    } 
    
    //C Program To Check Abundant Number
    #include <stdio.h> 
      
    int main()
    {
        int num=18, sum = 0; 
      
        // Note that this loop runs
        for (int i=1; i<=num/2; i++) 
        { 
            if (num%i==0) 
            { 
                // If divisors are equal,take only one 
                // of them 
                if (num/i == i) 
                    sum = sum + i; 
      
                else // Otherwise take both 
                { 
                    sum = sum + i; 
                    sum = sum + (num / i); 
                } 
            } 
        }
        // calculate sum of all proper divisors only 
        sum = sum - num; 
        if(sum>num)
        {
            printf("Yes, The number is Abundant Number");
        }
        else
        {
            printf("No, The number is not Abundant Number");
        }
    
        return 0;
    }
    

    Output

    Yes, The number is Abundant Number
    

    Recommended Programs

       Program to find factorial of a number
       Program to check armstrong number