For a programming assignment, I have to ask the user for a number and determine if it is an abundant number or not and report that back to the user.
For some reason, when I enter a number to test this program, the program abruptly crashes after entering the number.
Code:
bool abundantTest(int n){ // boolean function to test if a number is abundant
bool isAbundant = false; // boolean flag to determine if the number is abundant
int sum=0;
for (int i = 0; i < n; i++){ // loop to check for the proper divisors of the number 'n'
if (n % i == 0){
sum = sum + i;
}
}
if (sum > n){
return true;
}
else{
return false;
}
}
int main(){
int num;
std::cout << "Enter a number to test if it is an abundant number: \n";
cin >> num;
abundantTest(num);
if (abundantTest(num) == true){
std::cout << "The number " << num << " is an abundant number.\n";
}
else if(abundantTest(num) == false){
std::cout << "The number " << num << " is NOT an abundant number.\n";
}
else{
}
}
I'm not sure why it is crashing. If anyone has any insight into this issue, your help would be greatly appreciated.