programming-examples/c++/_Basic/Hcf & Lcm.cpp
2019-11-15 12:59:38 +01:00

36 lines
801 B
C++

#include < iostream.h >
long gcd(long, long);
int main()
{
long x, y, hcf, lcm;
cout<<"Enter two integers\n";
cin>>x>>y;
hcf = gcd(x, y);
lcm = (x*y)/hcf;
cout<<"Greatest common divisor of "<<x <<"and"<<y<<" = "<<hcf<<"\n";
cout<<"Least common multiple of "<< x <<"and "<< y<<"= "<<lcm<<"\n";
return 0;
}
/*if 1st no is 0 then 2nd no is gcd
make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd*/
long gcd(long x, long y)
{
if (x == 0)
{
return y;
}
while (y != 0)
{
if (x > y)
{
x = x - y;
}
else
{
y = y - x;
}
}
return x;
}