Input a positive integer from the user and determine whether the number is a perfect number or not.
Note: A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the
sum of its positive divisors excluding the number itself.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Please, enter a number to check: ";
cin >> num;
int sum=0;
for (int i = 1; i < num; i++)
{
int d = num%i;
if (d == 0)
sum += i;
}
if (sum == num)
cout << num << " is a perfect number";
else
cout << num << " is not a perfect number";
}
Comments
Leave a comment