Help with GCF and LCM

I am trying to create a program that finds the GCF and LCM of two positive numbers using functions. I have fairly confused my self and would like a little assistance.

Here is my code so far.
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
#include <cstdlib>
#include <iostream>
using namespace std;
bool done;
int main()
{
int gcf
int lcm
int x,y
int Least
int Greatest
int n1,n2       
    
    int n1 = 0, n2 = 0; // Inputs to GCF.
	cout << "This program calculates the gcf of two positive integers";
    cout << endl;
    cout << "Enter the two POSITIVE INTEGERS. ";
    cin >> n1;
    cin >> n2;
    Greatest=1;
    Least=2;
    done = false;
    while (done == false)
	while(n1!=n2)

{
	if ((n1>0) && (n2>0))//checks if user has entered a negative number or numbers
	done = true;
	else 
{
	cout << "Enter two POSITIVE numbers: ";
    cout << endl;
    cin >> n1;
    cin >> n2;
    //cout << "GCF = " << gcf(a, b) << endl
    cout <<"The LCM is:" << Least << endl;
    cout << "The GCF is:"<< Greatest << endl;
    //system("PAUSE");
    return 0;
}

int gcf(int a, int b)
{
	int Greatest;
	int x=0;
	int y=0;
}
    for( int x=1; x <= n1; ++x)
{
    if (((n1 % x) == 0) && ((n2 % x) == 0)) 
{
	Greatest=x;
	y=n1;
}
}
	while ( (y % n1 != 0) || (y % n2 != 0) )     
{
    y = y + 1;  
}
	Least = y;
	return 0;
}
     
}
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
// Greatest Common Divisor and Lowest Common Multiple
#include <iostream>
using namespace std;

int main()
{	
	int  a, b;
	int GCD, LCM;
	cout << "input two values to calculate GCD and LCM:\n";
	cin >> a >> b;

	for(int i=a; i>=1; i--)
	{
		if (a%i==0 && b%i==0)
		{
			GCD = i;
			break;
		}
	}
	
	LCM = a*b/GCD;
	cout << "GCD = " << GCD << endl
	     << "LCM = " << LCM << endl;	
	
return 0;
}
@anup30
How does that help OP? (All it does is give code that doesn't match or help with the OP's code.)

@noahthedominator
You've got WAY too many variables in there, and the code doesn't compile. I recommend you start again with something that compiles. Every time you add something to your program, make sure it compiles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int gcf( int a, int b )
{
  return 1;
}

int lcm( int a, int b )
{
  return 1;
}

int main()
{
}

Now you can focus on putting in code.

In main(), the very first thing you want to do is get integers from the user. Don't worry right now about bad inputs.

1
2
3
4
5
6
7
int main()
{
    int n1, n2;
    cout << "This program calculates the gcf of two positive integers\n";
    cout << "Enter the two POSITIVE INTEGERS.\n";
    cin >> n1 >> n2;

And show the user the desired information:

1
2
3
4
5
6
7
8
9
10
int main()
{
    int n1, n2;
    cout << "This program calculates the gcf of two positive integers\n";
    cout << "Enter the two POSITIVE INTEGERS.\n";
    cin >> n1 >> n2;

    cout << "GCF = " << gcf( n1, n2 ) << "\n";
    cout << "LCM = " << lcm( n1, n2 ) << "\n";
}

This compiles and works -- only the answers are always 1. We need now to write the internals of the functions gcf() and lcm().

The GCD can easily be calculated using a recursive function according to the formula here:
http://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm

Once you get that working, the LCM can be computed using the GCD, as per the formula here:
http://en.wikipedia.org/wiki/Least_common_multiple#Reduction_by_the_greatest_common_divisor

When you run your program, try to see what happens for values:
  12 and 15 (which will give you 3 and 60)
  1 7 (1 and 7)
  0 2 (0 and 0 -- this will require you to add code to your lcm() function to fix it)

Hope this helps.
Last edited on
Thank you very much for you help. I have to find GCF not GCD though.

However your input was still helpful.

Heres what I have now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int gcf( int a, int b )
{
  return 1;
}

int lcm( int a, int b )
{
  return 1;
}

int main()
{
int n1, n2;
    cout << "This program calculates the gcf of two positive integers\n";
    cout << "Enter the two POSITIVE INTEGERS.\n";
    cin >> n1 >> n2;

    cout << "GCF = " << gcf( n1, n2 ) << "\n";
    cout << "LCM = " << lcm( n1, n2 ) << "\n";
}


How and where do I call the functions? I already found GCF and LCM without using functions. How can I implement the code to find the two into my current program with functions?
I've already answered that.

Now you need to read the links I gave you and look at the formulas. (If you had, you'd know that GCF and GCD are the same thing.)

Good luck!
Topic archived. No new replies allowed.