programming-examples/c++/Basic/Hcf & Lcm.cpp

36 lines
801 B
C++
Raw Normal View History

2019-11-18 14:44:36 +01:00
#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;
}