Is Perfect Number
This algorithm checks whether a given number is perfect number or not. Perfect number is a number which is equal to sum of its divisor. For example divisors of 6 are 1,2 and 3. The sum of these divisors is 6. So 6 is called as perfect number.
/*****Please include following header files*****/
// stdbool.h
/***********************************************/
bool IsPerfectNumber(int number) {
int sum = 0;
int i = 1;
while (i < number) {
if (number % i == 0)
sum += i;
++i;
}
return sum == number;
}
Example
bool isPerfectNumber = IsPerfectNumber(28);
Output
true