By: Prof. Dr. Fazal Rehman | Last updated: March 3, 2022
Write a C++ program to find HCF (Highest Common Factor) of two numbers Using Class Objects.
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.
C++ HCF Class diagram
C++
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
#include<iostream>
usingnamespacestd;
classT4TUTORIALS
{
private:
intn1,n2,i,gcd;
public:
voidin()
{
cout<<"Please Enter two integer values:"<<endl;
cin>>n1>>n2;
}
voidcalc()
{
for(i=1;i<=n1&&i<=n2;i++)
{
if(n1%i==0&&n2%i==0)
gcd=i;
}
}
friendintdisp(T4TUTORIALS);
};
intdisp(T4TUTORIALS obj)
{
cout<<"GCD is "<<obj.gcd<<endl;
}
intmain()
{
T4TUTORIALS obj;
obj.in();
obj.calc();
disp(obj);
}
Output
Please Enter two integer values:
3
7
GCD is 1.
FAQ
GCD of three numbers in C++ by using class and objects in object-oriented programming (OOP).
GCD of two numbers by using class and objects in object-oriented programming (OOP).
GCD program in c without recursion by using class and objects in object-oriented programming (OOP).
GCD of two numbers by using class and objects in object-oriented programming (OOP).
HCF of three numbers in C++ by using class and objects in OOP.
HCF of two numbers by using class and objects in OOP.
HCF example using classes and objects.
GCF example using classes and objects.
HCF program in c without recursion by using class and objects in OOP.
HCF of two numbers by using class and objects in OOP.
Develop the highest or greatest common factor among the numbers by using class and objects in OOP.