Printing to the screen.

This code is supposed to find all of the perfect numbers up to 100,000. I can get it to compile but I can't get anyhting to print to the screen.

#include <iostream>
#include "math.h"
using namespace std;
int main()
{
unsigned int n = 100000,sum = 0, num = 0, value = 0;
for(int num; num <= n; num++)
{
sum = 0;
for(int i = 1; i <= (num/2); i++)
{
if(num%i == 0) //True is i is a factor of num
{
sum+=i;
}
}


if(sum == num)
{
cout << num << "::" << endl;
for (int divisor = 1; divisor <= (num/2); divisor++)
{

if (num%divisor == 0);
cout << num << "::" << divisor << "+";

}
cout << "\b=" << num << endl;
}

}

return 0;
}
closed account (jwC5fSEw)
First: please put the code in code tags, it makes it much more readable.

Your issue is that all of the couts are inside your if statement, which never gets executed. sum will never equal num because sum is repeatedly set to 0 at the beginning of your first for-loop.
Topic archived. No new replies allowed.