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.
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;
}
#include <iostream>
usingnamespace 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
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;
elseif(sum > number)
return 1;
elsereturn -1;
}
output of above function will be 0 if perfect, 1 when abundant, -1 when deficient