@
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.