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.
#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;
}
Comments
Leave a comment