The code won't compile. There are unmatched opening / closing braces.
The open brace at line 3 and also at line 14 don't have corresponding closing braces. The indentation doesn't particularly help to make the meaning clear.
Sorry to be critical, but I'd rather not try to offer help on what I think your actual code
might look like. It's easier for everyone to just post the actual code you are running.
OK, I decoded the question now, My guess is that this is the intended code:
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
|
int i;
int n;
int sum;
for (i=1; i<=1000; i++) // checks for 1 to 1000 for perfect numbers
{
n = 1;
sum = 0;
while (n<i)
{
if (i%n == 0)
sum = sum + n;
n++;
}
if (sum==i)
{
n = 1;
while (n<i)
{
if (i%n == 0)
cout << i << " = " << n << endl;
n++;
}
}
}
|
So how to fix it. Well, don't attempt all of the output on a single line. There are some parts which occur just once. Others appear repeatedly.
These occur just once:
so place those on a line by themselves.
This part occurs repeatedly (almost).
cout << " + " << n;
so place that inside the while loop at line 21.
Almost there now. The one remaining problem is that the string " + " should not be output for the first factor. There are several different ways to solve that. I'll leave that as something for you to think about.
So this is the improved and almost working code. See if you can fix it from here:
17 18 19 20 21 22 23 24 25 26 27 28 29
|
if (sum==i)
{
n = 1;
cout << i << " = ";
while (n<i)
{
if (i%n == 0)
cout << " + " << n;
n++;
}
cout << endl;
}
|