### Program does not give the right values.

We have to make a LCM function, the GCD function was given. I copied and pasted the GCF function to LCM function because I need the GCF and added lcd= (a*b)/c(which is the LCM) but it does not give the right values of the LCM

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` `````` 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"<>x; cout<<"Enter second integer"<>y; long result= gcd_euclid(x,y); cout<<"The result is: "<< result<< '\n'; cout<<" The GCD of "<< x<< " and "<
Last edited on
> I copied and pasted the GCF function to LCM function because I need the GCF
call the function
 ``123`` ``````long LCM(long a, long b){ return a*b/gcd_euclid(a,b); }``````
why use such a long and complicated program for lcm and gcd?

Here is a program i wrote a year back.
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; int lcm(int n1,int n2) { int max,lcm; max=(n1>n1; cout<<"Enter second number";cin>>n2; lcm(n1,n2); gcd(n1,n2); }``````
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include // std::abs (int) int calc_gcd( int a, int b ) { return b == 0 ? a : calc_gcd( b, a%b ) ; } int gcd( int a, int b ) { if( a==0 && b==0 ) return 0 ; // gcd(0,0) is not defined; // mimic the 'given' gcd function which returns zero // gcd is the largest positive integer that divides each of the integers // https://en.wikipedia.org/wiki/Greatest_common_divisor else return calc_gcd( std::abs(a), std::abs(b) ) ; } long long calc_lcm( int a, int b ) { // https://en.wikipedia.org/wiki/Least_common_multiple#Computing_the_least_common_multiple const long long aa = std::abs(a) ; // try to avoid potential signed integer overflow const int bb = std::abs(b) ; return aa*bb / gcd(aa,bb) ; } long long lcm( int a, int b ) { // LCM(a, b) is the smallest positive integer that is divisible by both a and b. // Since division of integers by zero is undefined, this definition has meaning // only if a and b are both different from zero. However, some authors // define lcm(a,0) as 0 for all a, which is the result of taking the lcm to be // the least upper bound in the lattice of divisibility. // https://en.wikipedia.org/wiki/Least_common_multiple if( a==0 && b==0 ) return 0 ; else return calc_lcm(a,b) ; }``````
Thanks for the help guys, I appreciate it.
Topic archived. No new replies allowed.