0

My IsPerfect function aint working and I dont no where i went wrong.

bool IsPerfect(int num)
{	
	int sum = 0;
	int x;
	int divisor;

	x = num;

	while(x > 0)
	{
		divisor = x;


		if(num % divisor == 0)
		{
			sum += divisor;
		}
		x--;
	}

	if(sum == num)
	{
		return true;
	}
	else
	{
		return false;
	}
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by John A
0

In the first iteration of your while() loop, you divide the number by itself, which will always divide evenly, thus making your sum twice as large as it should be.

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.