Classes and Objects C++ program to find the perfect numbers within a given range
//simple class #include<iostream> //header file using namespace std; //using namespace class perfect //class declaration with name perfect { //starting of class private: //it's a class accessed only by the function inside the class int num,i,st,end,sum; //integer type number, i, starting and ending number public: //function public is accessible outside the class void in() //void keyword specifies that function does not return a value { cout<<"Enter starting number="; //display starting number cin>>st; //take starting number from user cout<<"Enter ending ending number="; //display ending number cin>>end; //take ending number from user } void disp() //use to display a message on screen using void { //starting loop for(num=st;num<=end;num++) //this loop execute with having number equal to starting number and if the number is less than the ending number then increment in number until condition is true { i=1; sum=0; while(i<num) //i is less than number 1 and sum is 0 and i is less than number { if(num%i==0) //number divided by 2 and if equal to 0 than it is even number sum=sum+i; //than value of i added in sum i++; //increment in i } if(sum==num) //if sum equal to number cout<<"The perfect number within the given range="<<num<<endl; //it display the number is in given range } } //loop end }; int main() //main funtion { perfect obj; // object of main class perfect obj.in(); //assign object to int obj.disp(); //assign object to display function } //ending the main function
Advertisement
Advertisement