C++ Program to check whether a number is an Abundant Number or not

Abundant Number in C++

We will learn different ways of Abundant Number in C++

Abundant number is an number in which the sum of the proper divisors of the number is greater than the number itself.

Ex:- Abundant number 12 having a proper divisor is 1, 2, 3, 4, 6 

The sum of these factors is 16 it is greater than 12 
So it is an Abundant number

Some other abundant numbers: 

18, 20, 30, 70, 78, 80, 84, 90, 96, 100, 104, 108, 120
Abundant number
Abundant number in C++

Methods

  1. Finding divisors by traversal between [1, num-1]
  2. Finding divisors by traversal between [1, √num-1]

Method 1

C++ Code:-

Run
#include <iostream>
using namespace std;

int main ()
{
    int n = 12, sum = 0;
    
    for(int i = 1; i < n; i++) { if(n % i == 0) sum = sum + i; } if(sum > n){ 
cout << n << " is an Abundant Number\n"; 
cout << "The Abundance is: " << (sum-n);
 } else 
cout << n << " is not an Abundant Number\n"; 
} 
// Time complexity: O(N)
// Space complexity: O(1)

Output

12 is an Abundant Number
The Abundance is: 4

Method 2

C++ Code:-

Run
#include <iostream>
#include <math.h>
using namespace std; 

int getSum(int n){
    
    int sum = 0;
    
    for(int i = 1; i < 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; 
} 
int main() { 
int n = 12; 
int sum = getSum(n); 
if(sum > n) { 
cout << n << " is an Abundant Number\n";
 cout << "The Abundance is: " << (sum-n); 
} else
 cout << n << " is not an Abundant Number\n"; 
} 
// Time Complexity: O(√N) 
// Space Complexity: O(1)

Output

12 is an Abundant Number
The Abundance is: 4

Prime Course Trailer

Related Banners

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