Perfect Number Finder

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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>

using namespace 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;
}
Last edited on
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

https://en.wikipedia.org/wiki/List_of_perfect_numbers
Last edited on
Hold on a sec.... And thanks for the embarrassing correction.
:-) ...!
Topic archived. No new replies allowed.