gcd problem

I am trying to build a recursive gcd (number1, number2) function and I think I have most of it.
However, the directions say that number1 must always be larger then number2.  I believe I have this too, ut when I was running my test plan I came across a snag.

I am not sure if I have done this correctly... because when I enter gcd(73,73) there is no output.  Any ideas on what I'm missing?  Also I am not sure if it is exchanging the two integers when number2 is larger??!!!!!


int gcd (int number1, int number2)
{
	if (number1 % number2 == 0)
		return number2;
	else
		return gcd (number2, number1 % number2);
	
}
 
int main ()
{
	//introduce myself
	cout << "Project 1: Exercise 6.45  Created by Ali Ideus-Balboa" << endl;
	cout << "Determine the GCD of two integers where number1 is always larger then number2. " << endl;
	cout << endl;
 
	int number1;	//declare first integer value
	int number2;	//declare second integer value
 
	cout << "Enter he first number (integer): ";	//read in first integer input by user
	cin >> number1;
	cout << "Entr the second number (integer): ";	//read in second integer input by user
	cin >> number2;
 
	if (number1 == number2)		//swap number1 and number2 if first number is not greater
		return number1;
	if (number1 < number2)
		number2 = number2 - number1;
	else 
		number1 = number1 - number2;	//at this point number1 will always be >= number2
 
	cout << gcd(number1, number2) << endl;
}

Open in new window

rebelcowgirl_73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>         if (number1 == number2)         //swap number1 and number2 if first number is not greater
>>                 return number1;

You don't need this. Your gcd function already correctly handles the situation where number1 == number2. Just get rid of these two lines.


>>         if (number1 < number2)
>>                 number2 = number2 - number1;
>>         else
>>                 number1 = number1 - number2;    //at this point number1 will always be >= number2

This is not a good way to swap. Swapping is usually done like this :

        if (number1 < number2) {
            int tmp = number1;
            number1 = number2;
            number2 = tmp;
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rebelcowgirl_73Author Commented:
Thanks!!!!  Again!!!!!!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.