#include <iostream>
usingnamespace 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;
}
}
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:
#include <iostream>
usingnamespace 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.