Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Check whether a given number is Abundant or not

C++ Numbers: Exercise-2 with Solution

Write a program in C++ to check whether a given number is Abundant or not.

Pictorial Presentation:

C++ Exercises: Check whether a given number is Abundant or not

Sample Solution:

C++ Code :

#include <bits/stdc++.h>
using namespace std;
int getSum(int n)
{
    int sum = 0;
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i==0)
        {
            if (n/i == i)
                sum = sum + i;
            else // Otherwise take both
            {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
    sum = sum - n;
    return sum;
}
bool checkAbundant(int n)
{
    return (getSum(n) > n);
}
int main()
{
int n;
 cout << "\n\n Check whether a given number is an Abundant number:\n";
 cout << " --------------------------------------------------------\n";
cout << " Input an integer number: ";
cin >> n;
    checkAbundant(n)? cout << " The number is Abundant.\n" : cout << " The number is not Abundant.\n";
    return 0;
}

Sample Output:

Check whether a given number is an Abundant number:                                                 
 --------------------------------------------------------                                            
 Input an integer number: 35                                                                         
 The number is not Abundant.

Flowchart:

Flowchart: Check whether a given number is Abundant or not

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to check whether a given number is an Ugly number or not.
Next: Write a program in C++ to find the Abundant numbers (integers) between 1 to 1000.

What is the difficulty level of this exercise?