Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An integer is said to be a perfect number if the sum of its divisors, including

ID: 3725071 • Letter: A

Question

An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function isPerfect that determines whether its parameter (type int) is a perfect number and returns a bool. Use your function in a test program that prompts the user to enter the lower and upper boundaries for where to look for the perfect numbers and then outputs all perfect numbers in the given range (including the endpoints).

Explanation / Answer

#include <iostream>
using namespace std;

int main() {

    int min,max,i,j,number,factorSum;

    cout<<" Enter the range : ";
    cin>>min>>max;

    for(j=min;j<=max;j++)
    {
    //find factors and the sum of factors of number
    number = j;
    factorSum = 0;
    for(i=1;i<j;i++)
    {
    if (number%i == 0 )
        factorSum += i;
    }
    //cout<<" FactorSum "<<factorSum<<" number "<<number;
    if(factorSum == number)
    cout<<" "<<number <<" is perfect";
    else
    cout<<" "<<number<<" is not perfect";
    }
   
    return 0;
}

Output:

Enter the range :1 50
1 is not perfect
2 is not perfect
3 is not perfect
4 is not perfect
5 is not perfect
6 is perfect
7 is not perfect
8 is not perfect
9 is not perfect
10 is not perfect
11 is not perfect
12 is not perfect
13 is not perfect
14 is not perfect
15 is not perfect
16 is not perfect
17 is not perfect
18 is not perfect
19 is not perfect
20 is not perfect
21 is not perfect
22 is not perfect
23 is not perfect
24 is not perfect
25 is not perfect
26 is not perfect
27 is not perfect
28 is perfect
29 is not perfect
30 is not perfect
31 is not perfect
32 is not perfect
33 is not perfect
34 is not perfect
35 is not perfect
36 is not perfect
37 is not perfect
38 is not perfect
39 is not perfect
40 is not perfect
41 is not perfect
42 is not perfect
43 is not perfect
44 is not perfect
45 is not perfect
46 is not perfect
47 is not perfect
48 is not perfect
49 is not perfect
50 is not perfect

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote