Could anybody please tell me why this programme won't work

Write your question here.
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
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>
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;
}
}
Last edited on
Do not double-post; it will not help your question be answered sooner.
https://cplusplus.com/forum/general/276845/
Last edited on
@missesIrby,
PLEASE 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 thus also easier to respond to your post.

Tutorials on how to use code tags:

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

I found the second link to be the most help.

Hint: You can hit "edit post", highlight your code and then 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.

It just makes it easier for us to view your code and edit it. Mainly what helps is to have the code readable though.
Thanks!
max

Edit:
Ok, since I'm nice, I'll show you what I mean:
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
#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;
}

See that? Very easy to read and see potential issues!

Ok, when I compiled your program, I got these errors:
untitled.cc:15:19: warning: variable 'i' used in loop condition not modified in loop body [-Wfor-loop-analysis]
                for (int u = 1; i <= 500; u++)
                                ^
untitled.cc:22:4: warning: variable 'u' is incremented both in the loop header and in the loop body [-Wfor-loop-analysis]
                        u++;
                        ^
untitled.cc:15:29: note: incremented here
                for (int u = 1; i <= 500; u++)
                                          ^
untitled.cc:9:6: warning: unused variable 'i' [-Wunused-variable]
        int i = 1, u = 1, sum = 0;
            ^
3 warnings generated.

That should give you an idea of where to start.

Good luck!
max
Last edited on
Thank you a lot! I really appreciate this :))
Topic archived. No new replies allowed.