An integer is said to be a perfect number if the sum of its divisors, including
ID: 3541504 • 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 parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the divisors of each perfect number to confirm that the number is indeed perfect. Challenge ther power of your computer by testing numbers much larger than 1000.
Explanation / Answer
#include <iostream>
using namespace std;
void perfectNumber(int num)
{
int sum=0;
for(int i=1;i<num;i++)
{
if(num%i==0)
{
sum = sum + i;
}
}
if(sum==num)
{
cout<<endl<<num <<" is Perfect number formed with below divisors:"<<endl;
for(int i=1;i<num;i++)
{
if(num%i==0)
{
cout<<i<<" ";
}
}
}
}
int main()
{
for(int j=1;j<=1000;j++)
{
perfectNumber(j);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.