perfect number

Jan 26, 2011 at 11:53am
i m a bigginer in c++. I need to write a complete C++ program that allows the user to enter a positive integer and then displays the proper divisors of the number, the sum of the proper divisors, and a message indicating whether the number is perfect, deficient, or abundant.

The case is: perfect number is a positive integer such that the sum of the proper divisors equals the number. Thus, 28 = 1 + 2 + 4 + 7 + 14 is a perfect number. If the sum of the divisors is less than the number, it is deficient. If the sum exceeds the number, it is abundant.

Can anyone help me with it pls?
Jan 26, 2011 at 12:25pm
Post what code you have written so far and we can walk you through it.
Jan 26, 2011 at 12:48pm
here is my code. its totally messed up. SORRY. i am not good at all

#include <iostream>
using namespace std;

int perfect, limit, divisor;
cout << "Please enter a positive integer: " ;
cin >> perfect;
cout << endl;
bool is_perfect(int number)
{
int limit = sqrt((double)number);
int sum = 1;
for (int i=2; i<=limit; i++)
if (number % i == 0)
sum += i + number/i;
return sum == number;
}
divisor = 1;

while (limit < perfect);
{
if ((perfect % divisor) == 0);
{
divisor = prevtemp;
temp = prevtemp + temp;
}
limit++; divisor++;
}
if (perfect == temp)
cout << "Your number is a perfect number!" << endl;
else
cout << "Your number is not a perfect number" << endl;
return 0;
}
Jan 26, 2011 at 4:44pm
Where is your main function?

Edit: I would use a function for finding the sum of proper divisors of a number, then check if it's perfect like this:
1
2
if (number == sum_of_factors(number))
    cout << "Number is perfect.\n";
Last edited on Jan 26, 2011 at 4:50pm
Jan 26, 2011 at 5:58pm
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
26
27
28
29
30
#include <iostream>
using namespace std;

int main()
{
    int perfect, limit, divisor;
    cout << "Please enter a positive integer: " ;
    cin >> perfect;
    cout << endl; 

    bool isPerfect = is_perfect(perfect);

    //continue on here

    return 0;
}

bool is_perfect(int number)
{
    int limit = sqrt((double)number);
    int sum = 1;
    for (int i=2; i<=limit; i++)
    {
           if (number % i == 0)
           {
                 sum += i + number/i;
                 return sum == number;
           }
     }
} 


this should help... the int main() function is what will run when you compile your code and run your program. the bool is_perfect(int number) is defined outside of main but it can be used inside of main

see what you can come up with on your own and post your modified code if you need more help
Jan 26, 2011 at 7:17pm
That's fine but if you want to check if a number is deficient, abundant, or perfect then you should do what I said.
Jan 26, 2011 at 8:24pm
For finding whether the number is deficient, abundant or perfect, we can just modify the is_perfect(int number) method suggested by ceruleus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int is_perfect_deficient_abundant(int number)
{
    int limit = sqrt((double)number);
    int sum = 1;
    for (int i=2; i<=limit; i++)
    {
           if (number % i == 0)
                 sum += i + number/i;
     }

     if(sum == number)
           return 0;
     else if(sum > number)
           return 1;
     else
           return -1;
}

output of above function will be 0 if perfect, 1 when abundant, -1 when deficient
Jan 26, 2011 at 8:49pm
You can replace the if statement above with return sum - number;
Topic archived. No new replies allowed.