-
Homework Help
I am new to C++ programming, and have this homework where I need to find all the perfect numbers to 1000.
I have worked on this problem for a while now, and really trying to get it figured out, but I'm doing something wrong. I was wondering if someone could turn my head as to what it is.
Code:
int testNum = 2; //the number to be tested to see if its perfect - i started it ith 2 since the perfect number must be the sum of all divisors excluding itself.
int testDiv; //this sets test divisors to see if it goes into the number
int accum = 0; // sums the divisors to see if they equal the test number
int counter = 2; // count started at 2 siunce I know that there are none at 1
while( counter < 1000)
{
for (int testDiv = 1; testDiv < testNum; testDiv++)
{
if(testNum % testDiv == 0)
{
accum += testDiv;
}
if (accum == testNum)
{
cout << testNum << " is a perfect number less
than 1000." <<endl;
}
}
counter++;
testNum++;
This is just a snippet of the code in question. Thanks in advance for any help you can give.
-
> int accum = 0;
Put this inside your while loop
> if (accum == testNum)
Move this outside your for loop
Perhaps....
-
Code:
cout << testNum << " is a perfect number less
than 1000." <<endl;
And put that all on one line, or it won't compile. Or you can split it:
Code:
cout << "hello world";
cout << "hello "
"world";
cout << "hello \
world";
cout << "hello "
<< "world";
One of those should do.