i can't get this programme to work

I can't figure out what's wrong, so could any of you please help me.Thank you all in advance
here is the code
#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Find the perfect numbers between 1 and 500:\n";
int i = 1, u = 1, sum = 0;
cout << "\n The perfect numbers between 1 to 500 are: \n";
for (int i=1;i <= 500;i++)
{
for (int u=1;i <= 500;u++)
{
if (u < i)
{
if (i % u == 0)
sum = sum + u;
}
u++;
}
if (sum == i) {
cout << i << " " << "\n";
}
i++;
u = 1;
sum = 0;
}
}
Hello missesIrby,

First make the program easier to read.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



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
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
    cout << "\n\n Find the perfect numbers between 1 and 500:\n";

    int i = 1, u = 1, sum = 0;

    cout << "\n The perfect numbers between 1 to 500 are: \n";

    for (int i = 1; i <= 500; i++)
    {
        for (int u = 1; i <= 500; u++)
        {
            if (u < i)
            {
                if (i % u == 0)
                    sum = sum + u;
            }
            u++;
        }

        if (sum == i)
        {
            cout << i << " " << "\n";
        }

        i++;
        u = 1;
        sum = 0;
    }

    return 0;  // <--- Not required, but makes a good break point.
}

"u" always increase by 2. In the for loop you could just "u += 2".

I can not speak to the if statements, butIi feel as they are not quite right. Someone who knows more about this part will have to address it.

Andy
> "u" always increase by 2. In the for loop you could just "u += 2".
there your are, «u» should increase by 1, but you are increasing it by 2
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
#include <iostream>

int main()
{
   std::cout << "The perfect numbers between 1 and 500:\n\n";

   for (int itr { 1 }; itr <= 500; ++itr)
   {
      int num { 1 };
      int sum { };

       while (num < itr)
      {
         if (itr % num == 0)
         {
            sum += num;
         }
         ++num;
      }

      if (sum == itr)
      {
         std::cout << num << " is a perfect number\n";
      }
   }
}

The perfect numbers between 1 and 500:

6 is a perfect number
28 is a perfect number
496 is a perfect number
Thank you a lot!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	const size_t MAXPER {500};

	std::cout << "The perfect numbers between 1 and " << MAXPER << ":\n\n";

	for (int itr {1}; itr <= MAXPER; ++itr) {
		int num {1}, sum { };

		for (; num < itr; ++num)
			sum += num * !(itr % num);

		if (sum == itr)
			std::cout << num << '\n';
	}
}

Topic archived. No new replies allowed.