Find HCF in C++ by R4R Team

HCF:
Stand for Highest common factor.

Example-
HCF of 8 and 12 is 4
HCF of 5 and 7 is 1
HCF of 5 ad 10 is 5

program:

#include < iostream>
using namespace std;
class r4r{
int a,b,lcm;
public:
int get_value()
{
cout<<"Enter first number"<< endl;
cin>>a;
cout<<"Enter second number"<< endl;
cin>>b;
}
int find_hcf()
{
int min,i;
if(a< b)
min=a;
else
min=b;
for(i=min;i>0;i--)
{
if(a%i==0 && b%i==0)
{
cout<<"HCF is "<< i;
break;
}
}
}
};
int main()
{
r4r obj;
obj.get_value();
obj.find_hcf();
return 0;
}


output-

Enter first number
10
Enter second number
12
HCF is 2




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!