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

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;
}
Last edited on
> I copied and pasted the GCF function to LCM function because I need the GCF
call the function
1
2
3
long LCM(long a, long b){
	return a*b/gcd_euclid(a,b);
}
closed account (1vf9z8AR)
why use such a long and complicated program for lcm and gcd?

Here is a program i wrote a year back.
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
#include<iostream>
using namespace std;
int lcm(int n1,int n2)
{
    int max,lcm;
    max=(n1<n2)?n2:n1;
    do
    {
    if((max%n1==0)&&(max%n2==0))
    {
        cout<<"LCM="<<max;
        break;
    }
    else
        max=max+1;
    }while(true);
}

int gcd(int n1,int n2)
{
    int gcd,limit;
    limit=(n1<n2)?n1:n2;
    for(int i=1;i<=limit;i++)
    {
        if((n1%i==0)&&(n2%i==0))
            gcd=i;
    }
    cout<<"GCD="<<gcd;
}

int main()
{
    int n1,n2;
    cout<<"Enter first number";cin>>n1;
cout<<"Enter second number";cin>>n2;
lcm(n1,n2);
gcd(n1,n2);
}
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
#include <iostream>
#include <cstdlib> // 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.