Write a function perfect that determines whether a number is a perfect number or not. This function should receive a number and return true if the number is perfect and false otherwise. 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 as shown in the sample output below.
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.
I try to code but not run...check it once my code
#include <iostream.h>
#include <conio.h>
int perfectNumber(i nt);
int main()
{
clrscr();
cout<<"this progame will print all the perfect numbers\n\n";
for(short v=2;v<=1000;v++ )
if(perfectNumbe r(v))
{
cout<<v<<" factors are:";
for(short c=1;c<v;c++)
if(v%c==0)
cout<<c<<" ";
cout<<endl;
}
getch();
return 0;
}
int perfectNumber(i nt num)
{
int sum=0, r;
for(short v=1;v<num;v++)
{
r=num%v;
if(r==0)
sum=sum+v;
}
if(sum==num)
return 1;
return 0;
}
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.
I try to code but not run...check it once my code
#include <iostream.h>
#include <conio.h>
int perfectNumber(i nt);
int main()
{
clrscr();
cout<<"this progame will print all the perfect numbers\n\n";
for(short v=2;v<=1000;v++ )
if(perfectNumbe r(v))
{
cout<<v<<" factors are:";
for(short c=1;c<v;c++)
if(v%c==0)
cout<<c<<" ";
cout<<endl;
}
getch();
return 0;
}
int perfectNumber(i nt num)
{
int sum=0, r;
for(short v=1;v<num;v++)
{
r=num%v;
if(r==0)
sum=sum+v;
}
if(sum==num)
return 1;
return 0;
}
Comment