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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#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(); } |