I am in dire need of help. I have written this program to output the first 4 perfect number, numbers whose divisors can be added to produce the original number, and the code just sits there and does nothing when ran.

#include<iostream>
using namespace std;
int main()
{	int counter=1;
	int num=2;
	int sum=0;
	while(counter<=4)
	{	int i=1;
		while(i<num)
		{	if(num%i==0)
				sum=sum+i;
			i=i+1;
		}
		if(sum==num)
		{	cout<<num<<endl;
			counter=counter+1;
			num=num+1;
		}
	}
	system ("PAUSE");
	return 0;
}

how do you get to the next num if you do not find a perfect number?

how do you get to the next num if you do not find a perfect number?

Geez, I feel stupid. the num=num+1 should be outside of the last if statement

That's one problem; there's another. When you're testing a new number, how does the variable 'sum' get back to zero?

Beware looking for any perfect number larger then 8128 (which is the 4th one): There are quite a few, but the next one after 8128 is over 33.5 million. http://en.wikipedia.org/wiki/Perfect_number ... (2^12)*(2^13 - 1) (Read that as 'two to the twelfth times (one less than two to the thirteenth)'

That was fun. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.