# perfect number

• 04-01-2010
mallyg34
perfect number
My program works fine and listed the 3 perfect numbers under 1000, but i need help with listing the factors of the perfect numbers. Can someone please help me out with this?

Code:

```#include <iostream> using namespace std; void main() {     int n = 1000,sum = 0;         int i;           cout << "Searching for perfect number less than 1000..." << endl;       for(int num = 2; num <= n; num++)     {           sum = 0;       for(i = 1; i < num; i++)     {       if(num%i==0)                     sum+=i;               }       if(sum == num)           cout <<  num << " Is perfect number" << endl;             } }```
• 04-01-2010
tabstop
You find the factors once, you can do it again.
• 04-01-2010
mallyg34
Quote:

Originally Posted by tabstop
You find the factors once, you can do it again.

I don't understand.
• 04-01-2010
tabstop
Quote:

Originally Posted by mallyg34
I don't understand.

You have a whole section of code devoted to finding the factors of a number. If you did it in one place, presumably you can also run that same code in another place to find the factors of a number (again).
• 04-01-2010
mallyg34
Can't I someway put a "cout" statement in a loop to get the factors instead of make the code extra long.
• 04-01-2010
cyberfish
You may want to fix the indentation so people will have an easier time reading your code. It may even help you discover a bug or two.
• 04-02-2010
Cogman
Quote:

Originally Posted by mallyg34
Can't I someway put a "cout" statement in a loop to get the factors instead of make the code extra long.

It depends on how you have to display it.

As was stated, you are already finding factors, so storing the found factors in an array/vector for later display would be the route that I would take.
(this can be done in 3, maybe 4 lines).
• 04-03-2010
mallyg34
Quote:

Originally Posted by Cogman
It depends on how you have to display it.

As was stated, you are already finding factors, so storing the found factors in an array/vector for later display would be the route that I would take.
(this can be done in 3, maybe 4 lines).

We havent learned arrays yet in this language so im sure I cant use it until we get to it. This how the output is suppose to look:

6 is a perfect number
Factors: 1, 2, 3
• 04-03-2010
Aisthesis
the code only becomes barely longer by using the same methods you used in one of the blocks you have.
and i agree totally with cyberfish about the need for improvement with regard to indentation practices.
• 04-03-2010
anon