Assignment - Perfect Numbers with Multiple functions

I have to write a program involving multiple function, and im having a lot of trouble even starting it.

The assignment is the following:

A perfect number is one which is equal to the sum of its divisors (excluding itself). For example, 6 is a perfect number (the divisors of 6 are 1, 2, 3, 6; sum = 6).

Write a function sumdiv that accepts an integer and returns the sum of the divsors of the integer that was pass (excluding the integer itself). Thus a call of sumdiv(6) would return 6; a call to sumdiv(15) would return 9 (= 1 + 3 + 5).

I know this will involve at least one other function besides main, but im not sure how to set this up. How do I figure out the divisors of any integer I enter? And then how do I test to see if they add back to the original integer?

Can anyone at least help me with the setup?

Thank you in advance for any help.
Last edited on
There is operator %, modulus, that returns remainder of division. You have had loops already, have you?
Ok, so far I've written this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;


int sumdiv (i);

int main()
{
    cout << sumdiv(6);
   return 0;
}

int sumdiv (int i){
int sum = 0

for(i = 1; i<=1,000); i++)
    for (int divisor = 1; divisor<i; divisor++)
        if(i%divisor==0)
        sum+= divisor;
        return sum;
}



But Im getting all sorts of error messages that I dont know what to do with. Whats wrong with the above program?
Ok, revised code.

But still not compiling

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
#include <iostream>
#include <cmath>

using namespace std;


int sumdiv (i);

int main()
{
cout << sumdiv(6);
   return 0;
}

int sumdiv (int i){

int sum = 0;
int divisor;

for(i = 1; i<=1000; i++){
    {for(int divisor = 1; divisor<=i; divisor++)
        if(int divisor<i && i%divisor==0)
            sum+= divisor;
            return sum;}

}



Anyone can help?
Last edited on
Anyone?!
start by fixing line 7. you missed int
start by fixing line 7. you missed int


Thank you. Revised code again:

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>
#include <cmath>

using namespace std;


int sumdiv (int i){
{

    int sum = 0;
    int n, i = 1;

for(i=1; i>n; i++){
        for(n = 1; n<i; n++)
            if (i%n==0)
                sum+= n;}
       
        return sum;
    }
}


int main()
{
    cout << sumdiv(6) << "\n\n";
    return 0;
}



No more error messages but its still giving me the wrong output. I entered the number "6" into main, so the output should also be 6. But no matter what number I input, the output is always 0. Whats the problem?
Last edited on
Nevermind. I figured it out. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;


int sumdiv (int i){

    int sum = 0;
    int n;

for(n = 1; n<i; n++)
            if (i%n==0)
                sum+= n;
                return sum;

    }

int main()
{
    cout << sumdiv(8) << "\n\n";
    return 0;
}
Two notes:

First on style. It is legal to have only one statement on a loop body or in if-clause, but it is more clear to use a brace-block.

Second: 1 divides every number. There is no need to calculate it, unless input is less than 1.

Therefore:
1
2
3
4
5
6
7
8
9
10
// Returns sum of divisors of param i
// The param i must be greater than 0
int sumdiv ( int i ) {
  int sum = 1;
  for ( int n = 2; n < i; ++n ) {
    if ( 0 == i % n ) {
      sum += n;
    }
  return sum;
}
Topic archived. No new replies allowed.