I'm trying to write a program that finds all Perfect numbers. Perfect numbers are numbers that when a chain of numbers, starting at 1, is added together, it equals a Perfect Number. That is pretty confusing, so here are a few examples:
1+2=3 = Perfect Number
1+2+3=6 = Perfect Number
1+2+3+4=10 = Perfect Number
1+2+3+4+5=15 = Perfect Number
2+3+4=7 = NOT Perfect Number
Anyway, my program says yes to some and no to others. This is a hard problem for me, I've been struggling with it for a while now. Anyone have a fix? Thanks!
#include<iostream>
usingnamespace std;
int main(){
int n, input, divise, sum = 0;
cout << " Enter a number to see whether or not it is a perfect number." << endl;
cin >> input;
for(n = 1; n < input; n++){
divise = input % n;
if(divise == 0)
sum = sum + n;
}
if(sum == input)
cout << "\n" << input << " is a perfect number.";
else
cout << "\n" << input << " is not a perfect number.";
return 0;
}
Your understanding of perfect numbers is imperfect. A perfect number is equal to the sum of all of its divisors that are less than itself. The first five perfect numbers are:
6, 28, 496, 8128, 33550336