GCD/LCM function problem

Jan 28, 2012 at 6:11pm
I'm writing a program that calls a function that calculates and returns both the GCD and LCM of 2 integers. I am having an issue with what's going on in the function as far as the way to code the formulas, and to get it to return both the GCD and LCM...What does it look like I'm doing wrong?

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
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int GCDLCM(int a,int b);




int _tmain(int argc, _TCHAR* argv[])
{ 
	int num1, num2;

	cout<<"Enter two integers:  "<<endl;
	cin >> num1 >>num2;

    if (num1<0 || num2<0)
	{
		num1=abs (num1);
		num2=abs(num2);
	}

	cout << " You entered: " << num1<< " , " << num2<<endl;
	GCDLCM ( num1,num2 );
	cout<<endl;

	return 0;
}
int GCDLCM(int a,int b)
{
	int GCD=0, LCM;

	 
    
        //a = a % b;
		//if( a == 0 )
			//GCD = b;
		//b = b % a;

        //if( b == 0 )
			//GCD = a;
    

	if(b==0) // base case, the programs stops if y reaches 0.
    cout << a; //it returns the GCD
    else
    GCD = b* a%b; 

	LCM = (a* b)/ GCD;

	//cout << "The GCD is: "<<GCD <<" , The LCM is: " <<LCM;
	
	return  GCDLCM ( a, b);

}
Jan 28, 2012 at 10:50pm
why dont you break the GCFLCM into two different functions?
Jan 29, 2012 at 12:06am
You've got a recursive function without a base case.
cout << a; //it returns the GCD No, it prints the GCD. In general you may want to use that value for other things, so it is kind of useless like that.
And if you say that your function returns 1 integer, then you can return just 1 integer.
Jan 30, 2012 at 2:38am
Ok, I am having an EXTREMELY difficult time trying to figure out why I can't find a way to code that Euclidean Algorithm into my function.. Kapo the function needs to do both GCD and 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
#include "stdafx.h"
#include <iostream>

using namespace std;

int GCDLCM(int num1,int num2);




int _tmain(int argc, _TCHAR* argv[])
{ 
	int num1, num2;

	cout<<"Enter two integers:  "<<endl;
	cin >> num1>>num2;

    if (num1<0 || num2<0)
	{
		num1=abs (num1);
		num2=abs(num2);
	}

	cout << " You entered: " << num1<< " , " << num2<<endl;
	GCDLCM ( num1,num2);
	cout<<endl;

	return 0;
}
int GCDLCM(int num1,int num2)
{
	int GCD, LCM, r=num1%num2;
	   
	while (r !=0)
	{r = num1 % num2;
	num1 =num2;
	num2=r;}
		
		
	//LCM = num1* num2/ GCD;

	//cout << "The GCD is: "<<GCD <<" , The LCM is: " <<LCM;
	
       return  0;
		 
}
Jan 30, 2012 at 5:13am
Kapo the function needs to do both GCD and LCM...

kapo is right a function should do one thing well........why must it do two things?
This is one way of getting the gcd:
long gcd(long m,long n)
{
if(m>n)swap(m,n);
assert(n>0);
while(n>0)
{
long r=m%n;
m=n;
n=r;
}
return m;
}
Feb 16, 2012 at 9:51am
Ok, I figured out how to come up with the GCD, but now the LCM isb't calculating correctly, It's always coming out as 0... what am I doing wrong?


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
67
68
69
70
71
72
73
#include "stdafx.h"
#include <iostream>
#include <cstdlib>


using namespace std;

void GCD_LCM(int a, int b);

int _tmain(int argc, _TCHAR* argv[])
{


	int num1, num2;
	char again = 'y';

	while (again == 'y' || again =='Y')
	{
	cout << "Enter first number: "<<endl;
	cin>>num1;
	cout <<"Enter second number: " <<endl;
	cin>> num2;

	   if (num1<0 || num2<0)
	{
		num1=abs(num1);
		num2=abs(num2);
	}

	cout<<endl;
	cout<< " The GCD and LCM for "<<num1<< " and " <<num2<< " is: ";
    GCD_LCM (num1,num2);
	cout<<endl;
	cout<<endl;
	cout<<" Do you want to run this program again? (Y/N)"<<endl;
	cin>> again;
	cout<< "================================================="<<endl;
	cout<<endl;
	}
	return 0;
}
   void GCD_LCM(int a, int b)
  {    
	   int q=a/b,r=a%b, GCD1,LCM, num1=0, num2=0;

	   a=b*q+r;
	   

	 if (r==0)
		 GCD1= a;

	 else 
		 while (r!=0)
		 {
		   r=a%b;
		   a=b;
		   b = r; 
		 }

		 GCD1= a;

      // LCM
	// if (GCD1=a)

		 LCM=( num1 *num2)/18;

	// else if (GCD1 = b)
	 
		 //LCM=(a *b)/GCD1;

	 	cout<< GCD1<< " and "<< LCM<<endl;

   }
Topic archived. No new replies allowed.