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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
long gcd_euclid(long a, long b)
{
if(a ==0 && b ==0){
return 0;
}
if(a<0,b<0){
return 0;
}
if(a==0 && b != 0){
return gcd_euclid(b,b);
}
if(b==0 && a !=0){
return gcd_euclid(a,a); }
if (a < b) {
return gcd_euclid(b,a);
}
long c = (a%b);
if (c == 0)
return b;
else if (c == 1)
return 1;
return gcd_euclid(b,c);
}
long LCM(long a, long b)
{
if(a ==0 && b ==0){
return 0;
}
if(a<0,b<0){
return 0;
}
if(a==0 && b != 0){
return LCM(b,b);
}
if(b==0 && a !=0){
return LCM(a,a); }
if (a < b) {
return LCM(b,a);
}
long c = (a%b);
if (c == 0)
return b;
else if (c == 1)
return 1;
long lcd =(a*b)/(c);
return LCM(lcd,lcd);
}
int main()
{
long x,y;
cout<<"Enter first integer"<<endl;
cin>>x;
cout<<"Enter second integer"<<endl;
cin>>y;
long result= gcd_euclid(x,y);
cout<<"The result is: "<< result<< '\n';
cout<<" The GCD of "<< x<< " and "<<y<<" is "<<gcd_euclid(x,y);
cout<<" The LCM of "<< x<< " and "<<y<<" is "<<LCM(x,y);
return 0;
}
|