I have to write a C++ program that finds all the perfect numbers between 1 to 10,000 and print out the factors after the code finds them. I have written the code to find the numbers, but cant seem to wrack my brain on how to make the factors print with them. Any help would be greatly appreciated!!

Here is what i have so far.

``````#include <iostream>
using namespace std;

void perfect(int);

int main()
{
for(int number=2; number <= 10000 ; number++)
perfect(number);
system("pause");
return 0;
}

void perfect(int number){
int total = 0;
int i = 1;
int saveNum = 0;
for (i = 1; i < number; i++)
{
if (number % i == 0)
total += i;

}
if (number == total)
cout << number << endl;
}``````

this prints out

6
28
496
8128

I just cant seem to figure out how to get the factors to save in the function so i can print them with their perfect numbers.

Thanks!!

I made little changes to you code. Just ask if you need me to explain. Its tricky when you first start thinking about it, but if you examine it closely its actually pretty easy.

``````#include <iostream>

using namespace std;

void perfect(int);

int main()
{
for(int number=2; number <= 10000 ; number++)
perfect(number);
return 0;
}

void perfect(int number)
{
int total = 0;

for (int i = 1; i < number; i++)
{
if (number % i == 0)
total += i;
}
if (number == total)
{
for (int x = 1; x < number; x++)
{
if (number % x == 0)
cout << x << " + ";
}
cout << " = " << number << endl;
}
}``````

That is perfect!! Thanks so much for your help, and i am slapping myself on the forehead cause i should have been able to figure that out lol.

That is perfect!! Thanks so much for your help, and i am slapping myself on the forehead cause i should have been able to figure that out lol.

Sometimes the most helpful thing in programming is a set of fresh eyes looking over the code! :)