A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6=3+2+1. The next is 28=14+7+4+2+1. There are four perfect numbers less than 10000. Write a program to find all these four numbers.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int num = 1;
int sum,divisor;
while(num<=10000)
{
sum = 0;
divisor=1;
while(divisor<=num/2)
{
if((num%divisor)==0)
{
sum+=divisor;
}
divisor++;
}
if(sum==num)
{
cout<<"Perfect numbers less than 10000: "<<num<<"\n";
}
num++;
}
return 0;
}