Hi there, my professor just assigned us a new assignment to write a algorithm that determines whether or not a number is deficient, perfect or abundant. A number is abundant is the sum of it's proper divisors (sum) is greater than the original number. It's deficient if the sum is less than the original number and perfect if the sum is equal to the original number.
He wants us to write it so that it repeatedly reads a positive integer, determines if the integer is deficient, perfect, or abundant, and outputs the number along with its classification.
He also said that you may assume that the input integers are greater than 1 and less than 32500.
Here's my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
using namespace std;
int main()
{ int n=1;
int sum=0;
while (n <= 32500){
cout << " Enter a number: "<< endl;
cin >> n;
for(int i=1;i<n;i++)
if (n / i * i == n){
sum+=i;
if (sum < n){
cout<<n<<" is a deficient number"<<endl;}
else if (sum > n){
cout<<n<<" is a abundant number"<<endl;}
else if (sum == n) {
cout<<n<<" is a perfect number"<<endl;}
}
}
return 0;
}
| |
The problem with it is that it doesn't output just one classification, for example if my input is 6 (which is a perfect number) it outputs
6 is a deficient number
6 is a deficient number
6 is a perfect number |
I tried changing my code to else if's (as you can see above) but it didn't fix the problem. I can't seem to figure out why it outputs multiple classifications
Any help would be greatly appreciated.