Perfect number program question - help in printing out in a different format

I'm working on a program to find perfect numbers but have run into a roadblock in printing the results. How do I change the cout part so that the lines print out like this:
6 = 1 + 2 + 3
instead of like this:
6 = 1
6 = 2
6 = 3

Thanks for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
    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++;
    }
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:
 
cout << i << " = ";
 
cout << endl;

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;
        }
Last edited on
Woops, sorry, when I copied and pasted I guess I cut off a couple of the braces. Thank you for the constructive critique on the code. That's the only way to learn properly. :)

Ok, I made the changes you suggested and have figured out how to move the + sign from the front of the first factor and get everything to print on separate lines as well.

Thanks so much for your help!!

Last edited on
Ok, I have tried everything I can think of to get the + sign to print correctly but no matter what I try or where I put it in the code, it still prints numbers out with a + at the end:

1 + 2 + 3 +

How do I fix this please? Thanks.

I've tried:

cout << n << " + ";

and

cout << " " << n << " +";

in the loop and also moved the code around every which way to other parts of the program to no avail.
Maybe print the first number first (outside of the loop) and then print all of the other numbers with the + sign in front? (So print the first number first and then make the loop cout << " + " << n;)
AH HA! I figured it out. Thank you long double main! Your suggestion sparked an idea for me to try and it worked!! THANK YOU!!
Topic archived. No new replies allowed.