0

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;
}
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by griswolf
0

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

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.