Answer to Question #247389 in C++ for kanha

Question #247389

 Wap to create Hierarchical Structure. Number class is base data member int X and two derived class Perfect & Strong. It will check corresponding whether no is perfect or Strong. But thing is one input will taken and that single input will check both.



1
Expert's answer
2021-10-06T16:42:32-0400
#include <iostream>
using namespace std;
class Number {
   public:
      void setNumber(int w) {
         Number = w;
      }
      
      
   protected:
      int Number;
};
class Strong: public Number {
   public:
      int getStrong() { 
         int w,sum=0;
        cout<<"Enter number to check for strong: ";
        cin>>w;
        int save=w;
        while(w)
        {
            int num=w%10;
            int fact = 1;
            for(int i=num;i>0;i--)
            {
                fact=fact*i;
            }
            sum+=fact;
            w/=10;
        }
        if(sum==save)
        {
            cout<<save<<" is a Strong Number";
        }
        else
        {
            cout<<save<<" is not a Strong Number";
        } 
      }
};
class Perfect: public Number {
   public:
      int getPerfect() { 
         	int w, i, sum = 0;
	
	cout << "\nPlease Enter the Number to check for Perfect: ";
	cin >> w;
	
	for(i = 1 ; i < w ; i++)
	{
		if(w % i == 0)
		{
			sum = sum + i;
		}
	}
			
	if(w == sum)
	{
		cout << w << " is a Perfect Number";
	}
	else
	{
		cout << w << " is Not a Perfect Number";
	}
      }
};


int main(void) {
   int w;
   Strong s;
   s.getStrong();
   Perfect p;
   p.getPerfect();


   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS