Write a Object Oriented C++ program using Inheritance to check whether a given number is perfect or not ?
#include<iostream>
using namespace std;
class perfect
{
public:
void setnum (int digit)
{
number = digit;
}
protected:
int number;
};
class chknumber : public perfect
{
public:
int sum=0;
void getperfect ( )
{
for ( int i=1; i<number; i++ )
{
if ( number%i = =0 )
{
sum += i;
}
}
}
int chksum( )
{
if (number = = sum)
cout << "Yes!!" << endl<< "the given number is perfect number" << endl << endl;
else
cout << "Not!!"<< endl << "the given number is not a perfect number" << endl;
}
};
int main( )
{
chknumber per;
int digit;
cout << "Enter a number to check perfect or not : ";
cin >> digit;
per.setnum(digit);
per.getperfect();
per.chksum();
return 0;
}
#include
ReplyDelete#include
using namespace std;
class a
{
public:
int total=0;
};
class b : a
{
public:
void opt()
{
for(int i = 1; i<=100; i++)
{
for( int j=1; j<=i/2; j++)
{
if (!(i%j))
{
total+=j;
}
}
if (i==total)
{
cout << i << " is perfect";
}
//it works
total=0;
}
}
};
int main(void)
{
int opt();
}
Noted. No error. But very weak class found for inheritance. No work found in Class a, here only variable declaration. its funny.
Delete#include
ReplyDelete#include
using namespace std;
class var
{
public:
int i, j, total=0;
};
class inh : var
{
public:
void opt()
{
for(int i = 1; i<=100; i++)
{
for( int j=1; j<=i/2; j++)
{
if (!(i%j))
{
total+=j;
}
}
if (i==total)
{
cout << i << " is perfect";
}
total=0;
}
}
};
int main()
{
inh cls;
cls.opt();
}
This comment has been removed by the author.
Deletenoted. found correct. but weak concept. you have declared variable twice inside the class as like "int i". No need to declare twice, if it is inside the public section.
Delete//Md. Imam Uddin
ReplyDelete//CSE 46th Batch
//ID- 201420733
#include
using namespace std;
class perfect
{
public:
int a=0,d,i;
void get_valu()
{
{
cout<<"\n"<<"Enter the number:";
cin>>d;
}
for(i=1;i<d;i++)
{
if(d%i==0)
a=a+i;
}
}
};
class show:public perfect
{
public:
void result()
{
if(a==d)
{
cout<<d<<", "<<"Number is perfect: \n";
}
else
{
cout<<d<<", "<<"Number is not perfect:\n";
}
}
};
int main()
{
show mn;
mn.get_valu();
mn.result();
return 0;
}
noted and thanks. no compilation error. but little mistake in script. as like unnecessary bracket found as like line-11 and line-12. (after void get_valu() ). There are 2 { bracket started. it is useless.
DeleteDear Sir, Please visit below blog:
ReplyDeletehttp://itlearn24.blogspot.com/2014/11/check-perfect-number-using-inheritence.html
Name-Habibur Rahman
ReplyDeleteID-201421091
Batch-46
#include
#include
using namespace std;
class MainPower
{
public:
int i, j, total=0;
};
class inh : MainPower
{
public:
void devide()
{
for(int i = 1; i<=100; i++)
{
for( int j=1; j<=i/2; j++)
{
if (!(i%j))
{
total+=j;
}
}
if (i==total)
{
cout << i << "perfect Number";
}
total=0;
}
}
};
int main()
{
inh cls;
cls.devide();
}
Md. Khairul Hasan
ReplyDeleteBatch : 46th
ID No: 201420529
#include
#include
using namespace std;
class perfect
{
public:
int a=0,n,i;
void get_value()
{
{
cout<<"\n"<<"Enter the number:";
cin>>n;
}
for(i=1;i<n;i++)
{
if(n%i==0)
sum=a+i;
}
}
protected:
int sum;
};
class cal:public perfect
{
public:
void result()
{
if(a==n)
{
cout<<n<<", "<<"Number is perfect: \n";
}
else
{
cout<<n<<", "<<"Number is not perfect:\n";
}
}
};
int main()
{
cal mn;
mn.get_value();
mn.result();
return 0;
}
//ID:201421066
ReplyDelete//batch:46th
//progam:CSE
#include
using namespace std;
class p_n{
public:
int b=0,r,k;
void add_number(){
cout<<" "<<"please enter your code: ";
cin>>r;
for(k=1;k<r; k++)
{
if(r%k==0)
b=b+k;
}
}
};
class joint:public p_n{
public:
void add_result(){
if(b==r)
{
cout<<r<<" is perfect ";
}
else
{
cout<<r<<" is not perfect ";
}
}
};
int main()
{
joint object;
object.add_number();
object.add_result();
return 0;
}
//Area of square and cube in c++ program using inheritance
ReplyDelete//Md. Imam Uddin
//ID- 201420733
#include
#include
using namespace std;
class A
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter Number :\t";
cin>>a;
}
};
class B : public A
{
public:
void square()
{
getnumber();
cout<<"\n\n\tSquare of the number :\t"<<(a*a);
cout<<"\n\n\t-------------------------------";
}
};
class C :public A
{
public:
void cube()
{
getnumber();
cout<<"\n\n\tCube of the number :\t"<<(a*a*a);
cout<<"\n\n\t-------------------------------";
}
};
int main()
{
B ph;
ph.square();
C mn;
mn.cube();
getch();
}
sanchita das
ReplyDelete201420941
46th
//check strange number using inheritance
#include
using namespace std;
//base class
class strange
{
public:
void setdigit(int d)
{
digit=d;
}
protected:
int digit;
};
//derived class
class number:public strange
{
public:
int i,j,t=1,d;
int devided()
{
while(d!=0)
{
j=d%10;
d=d/10;
for(i=2;i>a;
nmb.setdigit(a);
nmb.check();
}
#include
ReplyDeleteusing namespace std;
int main()
{
int i,temp,d,revrs=0;
cout<<"Enter the number to cheek:";
cin>>i;
temp=i;
while(temp>0)
{
d=temp<0;
temp/=revrs*20+d;
}
if(revrs=i)cout<<i<<"is palindorme";
else
cout<<"is not palindorme";
}
#include
ReplyDeleteusing namespace std;
int main()
{
int a[10];
int i,s;
s=0;
cout<<"Enter values in array:\n";
for(i=0; i<10; i++)
{
cin>>a[i];
if(a[i]%2==0)
{
s=s+a[i];
}
}
cout<<"Sum of all even number in the array is:"<<s;
getch();
}
// Md Mizanru rahman
ReplyDelete// ID: 201420765
//check strange number using inheritance
#include
using namespace std;
class inher
{
public:
void innumber(int a)
{
neumeric=a;
}
protected: int neumeric;
};
class square: public inher
{
public:
int sqf;
void sqs()
{
sqf = neumeric * neumeric;
cout<<"the result of square :"<>a;
s.innumber(a);
s.sqs();
cout<<"\n\n enter the number of cube: ";
cin>>a;
cu.innumber(a);
cu.cufs();
return 0;
}
Name -Main Uddin
ReplyDeleteid-201421090
Batch-46
#include
using namespace std;
class PerNum1
{
public:
int M=0,G,X;
void Fine()
{
{
cout<<"\n"<<"Entry Number :";
cin>>G;
}
for(X=1;X<G;X++)
{
if(G%X==0)
M=M+X;
}
}
};
class Number:public PerNum1
{
public:
void NTD()
{
if(M==G)
{
cout<<G<<", "<<"perfect: \n";
}
else
{
cout<<G<<", "<<"not perfect:\n";
}
}
};
int main()
{
Number FF;
FF.Fine();
FF.NTD();
return 0;
}
This comment has been removed by the author.
ReplyDeleteMohammad Ashraful Hasan sobuj
ReplyDeleteCSE-46(Dip)
ID=201420656
#include
using namespace std;
class perfect
{
public:
void setelement(int value)
{
element =value;
}
protected: int element;
};
class chkelement: public perfect
{
public:
int sum=0;
void getperfect()
{
for(int i=1;i>value;
per.setelement(value);
per.getperfect();
per.chksum();
return 0;
}
Dear Sir, I have upload full program but not show some this program
ReplyDelete#include
ReplyDeleteusing namespace std;
class perfect
{
public:
int a=0,d,i;
void get_valu()
cout<<"\n"<<"Enter the number:";
cin>>d;
}
for(i=1;i<d;i++)
{
if(d%i==0)
a=a+i;
}
}
};
class show:public perfect
{
public:
void result()
{
if(a==d)
{
cout<<d<<", "<<"Number is perfect: \n";
}
else
{
cout<<d<<", "<<"Number is not perfect:\n";
}
}
};
int main()
{
show mn;
mn.get_valu();
mn.result();
return 0;
}
This comment has been removed by the author.
ReplyDeletemd.abul kashem
ReplyDeleteID- 201410856
45th
//program to split digit using inheritence
#include
using namespace std;
class split
{
public:
void getvalue(int a)
{
pp=a;
//cout<>s;
sh.getvalue(s);
sh.display();
return 0;
}
#include
ReplyDeleteusing namespace std;
class perfect
{
public:
void setnum(int digit)
{
number =digit;
}
protected: int number;
};
class chknumber: public perfect
{
public:
int sum=0;
void getperfect()
{
for(int i=1;i>digit;
per.setnum(digit);
per.getperfect();
per.chksum();
return 0;
}
sonia ferdusi
ReplyDeleteid:201410285,45th batch
#include
#include
#include
using namespace std;
int main()
{
int n;
cout<<"Enter the Number :";
cin>>n;
cout<<"List Of Prime Numbers Below "<<n<<endl;
for (int i=2; i<n; i++)
{
bool prime=true;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
prime=false;
break;
}
}
if(prime) cout << i << endl;
}
getch();
return 0;
}
#include
ReplyDeleteusing namespace std;
class perfect
{
public:
void set_input(int inp)
{
digit=inp;
}
protected:
int digit;
};
class number: public perfect
{
public:
int sum=0,i;
void search_perfect()
{
for(i=1;i>digit;
nm.set_input(digit);
nm.search_perfect();
nm.check();
return 0;
}
ID: 201510545 https://www.dropbox.com/s/sfd05ap27f9qyyf/PerfectNumber.cpp?dl=0
ReplyDelete