Write a program in C++ to input 10 numbers and print only abundant numbers.
An abundant number is the number for which the sum of its proper divisors(excluding the
number itself) is greater than the original number.
Sample input: 12
Sample output:
Its proper divisors are 1, 2, 3, 4 and 6
Sum = 1 + 2 + 3 + 4 + 6 =16
Hence 12 is an abundant number.
#include <iostream>
using namespace std;
bool isAbundantNumber(int number){
int sumDivisors=0;
for(int i=1;i<number;++i)
{
if(number%i==0){
sumDivisors+=i;
}
}
return (sumDivisors>number);
}
int main() {
for(int i=0;i<10;i++){
int number;
cout<<"Enter number: ";
cin>>number;
if(isAbundantNumber(number)){
cout<<number<<" is an abundant number.\n";
}
}
system("pause");
return 0;
}
Comments
Leave a comment