Write a C++ program to find HCF (Highest Common Factor) of two numbers Using single inheritance.
Greatest Common Divisor(GCD) or Highest Common Factor (HCF) of two numbers is the largest number that divides both of them. For example GCD of 36 and 54 is 18.
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 | #include<iostream> using namespace std; class A { protected: int n1,n2,i,HCF_t4tutorials; public: A() //constructor { n1=50; n2=40; } }; class B:public A { public: void calc() { for(i=1;i<=n1&&i<=n2;i++) { if(n1%i==0&&n2%i==0) HCF_t4tutorials=i; } } void disp() { cout<<"\nHCF of "<<n1<<" and "<<n2<<" is "<<HCF_t4tutorials<<endl; } }; int main() { B obj; obj.calc(); obj.disp(); } |
Output
HCF of 50 and 40 is 10.
FAQ
GCD of three numbers in C++ by using the single inheritance in object-oriented programming (OOP).
GCD of two numbers by using the single inheritance in object-oriented programming (OOP).
GCD program in c without recursion by using the single inheritance in object-oriented programming (OOP).
GCD of two numbers by using the single inheritance in object-oriented programming (OOP).
HCF of three numbers in C++ by using the single inheritance in OOP.
HCF of two numbers by using the single inheritance in OOP.
HCF example using classes and objects.
GCF example using classes and objects.
HCF program in c without recursion by using the single inheritance in OOP.
HCF of two numbers by using the single inheritance in OOP.
Develop the highest or greatest common factor among the numbers by using the single inheritance in OOP.