You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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;
}