#include <iostream>
#include <fstream>
bool is_perfect( int number )
{
if( number < 1 ) returnfalse ; // perfect numbers are positive
// there are no odd perfect numbers (at least none that that could be held in int)
// see: https://en.wikipedia.org/wiki/Perfect_number#Odd_perfect_numbersif( number%2 == 1 ) returnfalse ;
int aliquot_sum = 3 ; // sum of proper divisors
// initialised to 3 (even number, so one and two are proper divisors)
// check for divisibility with all integers from 3 up to n/2
for( int i = 3 ; i <= number/2 ; ++i )
if( number%i == 0 ) aliquot_sum += i ; // divisible, add to sum of divisors
return aliquot_sum == number ; // return true if it is a perfect number
}
void print_results( constint array[], int cnt )
{
for( int i = 0 ; i < cnt ; ++i )
{
std::cout << array[i] << ' ' ;
if( is_perfect( array[i] ) ) std::cout << "Yes\n" ;
else std::cout << "No\n" ;
}
}
int main()
{
constint SENTINEL = -1 ; // sentinel to signal end of input
constint ARRAY_SZ = 140 ;
int array[ARRAY_SZ] {} ; // initialise to all zeroes (not required, but often a good idea)
constchar file_name[] = "numbers.txt" ;
int cnt_numbers_read = 0 ; // count of numbers actually read from the file
std::ifstream file(file_name) ; // open file for input
// read up to a max of ARRAY_SZ numbers and place them in the array
// exit loop if a. ARRAY_SZ numbers have been read
// b. there are no more numbers in the file (input failed)
// c. the number that we read is the sentinel value
int number ;
while( /*a*/ cnt_numbers_read < ARRAY_SZ && /*b*/ file >> number && /*c*/ number != SENTINEL )
array[cnt_numbers_read++] = number ; // note: postfix increment of cnt_numbers_read
print_results( array, cnt_numbers_read ) ;
}