An integer is said to be a perfect number when the sum of itsdivisors (including
ID: 3617028 • Letter: A
Question
An integer is said to be a perfect number when the sum of itsdivisors (including 1) is equal to the number. Example: 6 is aperfect number because 6=1+2+3. Write a function perfect that determineswhether a number is a perfect number. The function should have aninteger as its only parameter and return the bool value trueif the number is perfect, false otherwise. Use this functionin a program that determines and displays all the perfect numbersbetween 1 and 1000. Hint: include diagnostic statements in the function to displaythe divisors of each perfect number to confirm that the number isindeed perfect. An integer is said to be a perfect number when the sum of itsdivisors (including 1) is equal to the number. Example: 6 is aperfect number because 6=1+2+3. Write a function perfect that determineswhether a number is a perfect number. The function should have aninteger as its only parameter and return the bool value trueif the number is perfect, false otherwise. Use this functionin a program that determines and displays all the perfect numbersbetween 1 and 1000. Hint: include diagnostic statements in the function to displaythe divisors of each perfect number to confirm that the number isindeed perfect.Explanation / Answer
#include<iostream> using namespace std;
bool perfect(int);
int main() { int v; for(v=1;v<1000;v++) { if(perfect(v)) cout<<v<<" is a perfect number"<<endl; } system("pause"); return 0; }
bool perfect(int num) { int c,sum=1; for(c=2;c<num;c++) { if(num%c==0) sum+=c; } if(sum==num) return true;
return false; }
#include<iostream> using namespace std;
bool perfect(int);
int main() { int v; for(v=1;v<1000;v++) { if(perfect(v)) cout<<v<<" is a perfect number"<<endl; } system("pause"); return 0; }
bool perfect(int num) { int c,sum=1; for(c=2;c<num;c++) { if(num%c==0) sum+=c; } if(sum==num) return true;
return false; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.