This is a relatively simple program thats supposed to list all the perfect numbers up to 500, along with the count next to each number. But for some reason its not working.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int i, n;
int sum = 0;
int cnt = 0;
for(int i=0; i<=500; i++){
for(int n = 1; n<i; n++){
if (i%n==0)
sum+=n;}
if (sum==i)
{cnt++;
cout << cnt << "\t" << i << endl;
}
}
return 0;
}
Previously I had set the sum to 0 within the brazes of the if statement. i just had to move it outside of the braces to separate it from the if-statement. My finalized, working program below: