# Perfect number

A number is called perfect if the number is equal to the sum of its divisors (the number itself is not considered a divisor in this case).

## Check Perfect Number

``````#include &lt;iostream&gt;
using namespace std;

int main() {
int number, sum = 0;

cout << "Enter a number to check for perfect: ";
cin >> number;

for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}

if (sum == number) {
cout << number << " is a perfect number";
}
else {
cout << number << " is not a perfect number";
}

return 0;
}``````
##### Output
``````Enter a number to check for perfect: 28
28 is a perfect number``````

## Code Explanation

The first two lines include the `iostream` library and the `std` namespace, which provides access to the standard input and output streams (`cin` and `cout`) used in the code.

``````void checkPerfect(int max) {
int number, min = 1, i, sum = 0;
``````

This is the definition of the function `checkPerfect`. It takes an integer argument `max`, which represents the upper bound for finding perfect numbers. Inside the function, several variables are declared:

• `number`: the number being tested to see if it’s a perfect number
• `min`: the lower bound for finding perfect numbers, initialized to 1
• `i`: a loop counter used in the inner loop to test the divisors of `number`
• `sum`: the sum of the divisors of `number`, initialized to 0
``````    for (number = min; number <= max; number++) {
sum = 0;
for (i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}
``````

The first for loop iterates over the numbers from `min` to `max`. Inside the loop, `sum` is reset to 0, and another for loop is used to test the divisors of `number`. The inner loop uses the modulo operator (`%`) to check if `i` is a divisor of `number`, and if so, adds `i` to `sum`.

``````        if (sum == number) {
cout << number << " is a perfect number" << endl;
}
``````

If the sum of the divisors of `number` is equal to `number` itself, then `number` is considered a perfect number, and is output to the screen.

``````    }
}
``````

The end of the `checkPerfect` function.

``````int main() {
int max;

cout << "Enter the upper bound to find perfect numbers: ";
cin >> max;

checkPerfect(max);

return 0;
}
``````

The `main` function is the entry point of the program. It first prompts the user to enter the upper bound for finding perfect numbers, then calls the `checkPerfect` function with the user’s input as an argument. The `return 0;` statement indicates that the program has executed successfully.

## Perfect Number List (1-n)

``````#include &lt;iostream&gt;
using namespace std;

void checkPerfect(int max) {
int number, min = 1, i, sum = 0;

for (number = min; number <= max; number++) {
sum = 0;
for (i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}
if (sum == number) {
cout << number << " is a perfect number" << endl;
}
}
}

int main() {
int max;

cout << "Enter the upper bound to find perfect numbers: ";
cin >> max;

checkPerfect(max);

return 0;
}``````
##### Output
``````Enter the upper bound to find perfect numbers: 10000
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number``````

## Code Explanation

``````void checkPerfect(int max) {
int number, min = 1, i, sum = 0;
``````

This is the definition of the function `checkPerfect`. It takes an integer argument `max`, which represents the upper bound for finding perfect numbers. Inside the function, several variables are declared:

• `number`: the number being tested to see if it’s a perfect number
• `min`: the lower bound for finding perfect numbers, initialized to 1
• `i`: a loop counter used in the inner loop to test the divisors of `number`
• `sum`: the sum of the divisors of `number`, initialized to 0
``````    for (number = min; number <= max; number++) {
sum = 0;
for (i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}
``````

The first for loop iterates over the numbers from `min` to `max`. Inside the loop, `sum` is reset to 0, and another for loop is used to test the divisors of `number`. The inner loop uses the modulo operator (`%`) to check if `i` is a divisor of `number`, and if so, adds `i` to `sum`.

``````        if (sum == number) {
cout << number << " is a perfect number" << endl;
}
``````

If the sum of the divisors of `number` is equal to `number` itself, then `number` is considered a perfect number, and is output to the screen.

``````    }
}
``````

The end of the `checkPerfect` function.

``````int main() {
int max;

cout << "Enter the upper bound to find perfect numbers: ";
cin >> max;

checkPerfect(max);

return 0;
}
``````

The `main` function is the entry point of the program. It first prompts the user to enter the upper bound for finding perfect numbers, then calls the `checkPerfect` function with the user’s input as an argument. The `return 0;` statement indicates that the program has executed successfully.