1. ## help with lists

Below is a program ive written to test if a number entered is a perfect number. However im having problems with making another function that will lists the perfect numbers from 1 to 1000. Any help or hints will be appreciated. Thanks

Code:
```#include <stdio.h>

int factors( int );
int perfect( int );

int main()
{
int number;

printf("Enter Number: " );
scanf( "%d", &number );

printf( "%d\n", perfect( number ) );

return 0;
}

int factors( number )
{
int counter = 1, counter2 = 0, newNumber;

while ( counter <= number ) {
if ( ( number % counter == 0 ) && ( number != counter ) ) {
newNumber = counter;
counter2 += newNumber;
}
counter++;
}

return counter2;
}

int perfect( number )
{
if ( number == factors( number ))
return 1;
else
return 0;
}```

2. Well you're going to have to use a loop. It will probably look something like this:

Code:
```/* Headers and forward declarations */
#define MAX_NUM 1000

int main()
{
int i;

for( i = 0; i < MAX_NUM; ++i )
{
if( perfect( i ) )
{
printf( "%d\n", i );
}
}

return 0;
}```
I think something like that will probably do it. I think you should be able to tell what is going on in the loop as it is fairly straight-forward. If you need help understanding it let me know. Basically we will execute the commands within the for loop braces until the middle condition ( i < MAX_NUM ) is false.

3. Ive implemented what you have said and understand what is going on but i think im still missing something, you have declared int number, but it's not doing anything..

4. Originally posted by kurz7
Ive implemented what you have said and understand what is going on but i think im still missing something, you have declared int number, but it's not doing anything..
Yeah you're right, I just copied and pasted your code for a starting point. You used number but I didn't. Take it out, you'll be fine. It should still work fine?

5. hmm not sure why its not working.
Just thinking about the code, will the counter always restart to 0 when the new number is called? Might keep incrementing so it will probably do 1 % 1, then 2 % 2 ...

i cuold be wrong

6. I didn't look at your perfect number algorithm at all. I was just showing you how to setup a for loop and use it. The loop index i should not be the problem. If you can't fix it by tomorrow let me know.

7. ive fixed the code to generate perfect numbers from 1 to 1000 but it still doesnt seem to be working.. not sure what the problem is, could be the counters.

Code:
```#include <stdio.h>
#define MAX_NUM 1000

int factors( int );
int perfect( int );

int main()
{
int i;

for( i = 0; i < MAX_NUM; ++i )
{
if( perfect( i ) )
{
printf( "%d\n", i );
}
}

return 0;
}

int factors( number )
{
int counter = 1, counter2 = 0, newNumber;

while ( counter <= number ) {
if ( ( number % counter == 0 ) && ( number != counter ) ) {
newNumber = counter;
counter2 += newNumber;
}
counter++;
}

return counter2;
}

int perfect( number )
{
if ( number == factors( number ))
return 1;
else
return 0;
}```

8. Program works fine for me. Start the loop counter at 1 instead of 0 because it reports 0 as a perfect number when in fact it is not. Again, program works for me.

9. hmm still doesnt seem to work, there should be 3 perfect numbers between 1 and 1000, did u get all 3? 6, 28, and another one ive forgotten..

it doesnt output anything

10. Output:
0
6
28
496

11. still doesnt work
is it a compiler problem? im on mandrake 8.2 and just using the gcc commands.

12. Originally posted by kurz7
still doesnt work
is it a compiler problem? im on mandrake 8.2 and just using the gcc commands.
Put some extra printf() statements in there to help you debug it. Also, try fflush(stdout); at the end of the program (shouldn't be necessary but you never know )