C++ program for perfect numbers between 1 and 1000

- March 14, 2018
A positive integer is said to be perfect if the sum of its proper divisors including 1 (but not the number itself) is equal to number. For example, 6 is a smallest perfect number because:
6 = 1 + 2 + 3
The following C++ simple program discovers perfect numbers between 1 and 1000.

Program Source Code

#include<iostream>
using namespace std;
main()
 {
  int i=1, u=1, sum=0;
  while ( i <=1000)
  {
   while (u <=1000)
   {
   if (u < i)
    {
     if ( i %u ==0 )
     sum = sum+ u;
     }
   u ++;
   }
   if (sum== i )
   {
   std:: cout << i<< " is a perfect number." <<"\n";
    }
   i ++; u =1; sum=0;
    }
 return 0;
 } 


Output of Program

6 is a perfect number.
28 is a perfect number.
496 is a perfect number.