An integer is said to be a perfect number if the sum of its divisors, including
ID: 3650836 • 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 1+2+3=6. Write a function PERFECT that determines whether a number is a perfect number. The function should have an integer as its only parameter and return the boolean value true if the number is perfect, false otherwise. Write a program that uses a for loop and calls this function to determine and display all the perfect numbers between 1 and 100.Hint: include diagnostic statements in the function to display the divisors of each perfect number to confirm that the number is indeed perfect.
Explanation / Answer
please rate - thanks
#include<iostream>
using namespace std;
bool PERFECT(int);
int main()
{int num;
cout<<"The perfect numbers between 1 and 100 are: ";
for(num=1;num<=100;num++)
{if(PERFECT(num))
cout<<num<<endl;
}
system("pause");
return 0;
}
bool PERFECT(int num)
{int sum,i,count=0;
count=0;
sum=0;
for(i=1;i<num;i++)
if(num%i==0)
{sum+=i;
count++;
}
if(sum==num)
{
for(i=1;i<num;i++)
if(num%i==0)
{count--;
cout<<i;
if(count>0)
cout<<" + ";
}
cout<<" = ";
return true;
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.