Deficient, Perfect, Abundant

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.
Last edited on
You need to learn to properly indent your code.
It looks like crap.
I'm sorry about that. I'm fairly new to coding, I'll make sure to work on it.
I think your output should be outside the for loop.
You mean like this?

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;

}
Yes, does it work now?
Partially, when I input 6 it outputs,

6 is a perfect number


like I want it to however, if I input another number as its in a while loop it doesn't output the right classification for the next number.

For example if I input 8, it outputs

8 is a abundant number


However, 8 is not a abundant number, its a deficient number.

Basically the first number I input in the loop has the right classification, however the others following it don't.
Last edited on
It's absolutely critical that you indent your code to match the actual block structure. Otherwise I can guarantee that someday you'll put a } in the wrong place and it will take you days to figure it out.

Don't

double

space

your

code.

It

makes

it

hard

to

read.

Occasional blank lines are encouraged, but don't take it too far.
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
#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;
}


Your code doesn't quite work right:
 Enter a number:
28
28 is a perfect number
 Enter a number:
28
28 is a abundant number


Can you see why? If not, then try printing sum and n right after the loop.
Thanks for your help,

I did as you said and I see that if I input 8, my sum is 7 and n is 8, however if I input another number let's say 10, my sum becomes 15, because it's adding the sum of the divisors of 10 (5,2,1) to the sum of the divisors of 8 right?

So using what I realized, I re declared every sum to 0 after an if statement and now it works!

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
#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;
	    sum = 0;
	}
	else if (sum > n) {
	    cout << n << " is a abundant number" << endl;
	    sum = 0;
	}
	else if (sum == n) {
	    cout << n << " is a perfect number" << endl;
	    sum = 0;
	}	
	
    }    
    
    return 0;
}


Btw, I completely agree with what you said about the indenting. Even while writing this code I had trouble matching up the brackets. It's definitely something I should start doing before I end up spending days figuring out my brackets.

Thanks!
Last edited on
> I can't seem to figure out why it outputs multiple classifications
because your output was inside the loop, so it will output in every iteration
bother yourself with drawing a flowchart or writing the pseudocode before start coding
Ah okay thanks! The next time I start an assignment I'll make sure to try writing steps out or making a flowchart before attempting to code.
Good job! You can make this even better by simply moving the declaration of int sum inside the while loop. Then you can remove the sum=0 inside the if statements:
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
#include <iostream>
using namespace std;
int main()
   {int n = 1;

    while (n <= 32500) {
	int sum = 0;
	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;
}

Registered users can post here. Sign in or register to post.