Write a C++ program to find the perfect numbers within a given range using classes and objects.
Perfect number:A number whose sum of “factors” is equal to the number itself is called perfect number e.g 6(1+2+3) or 28(1+2+4+7+14)
Factor:a number who exactly divides another number is called the factor of that number.
#include<iostream>
using namespace std;
class perfect
{
private:
int num,i,st,end,sum;
public:
void in()
{
cout<<"Enter starting number=";
cin>>st;
cout<<"Enter ending ending number=";
cin>>end;
}
void disp()
{
for(num=st;num<=end;num++)
{
i=1;
sum=0;
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
cout<<"The perfect number within the given range="<<num<<endl;
}
}
};
int main()
{
perfect obj;
obj.in();
obj.disp();
}