Ok i need a program that finds the greatest common divisor of two integers.

1) if A/B is 0 then B is the greatest common divisor

2) if A/B is not 0 then plug B into A and the remainder into B

3) repeat the process

I have to use one function other than main
and display both integers and the greatest common divisor

#include <iostream>
#include <cmath> 
using namespace std;

void A_B_Division  (int&, int&);

int main()
{
int A, B;

cout<<"Please enter two integers"<<endl;
cin<<A<<B;

cout<<"The integers "<<A<<" and "<<B<<" have the greatest common divisor ";

while (A/B==0)
{
cout<<B<<endl;
}

while (A/B!==0)
{
void A_B_Division (A, B);
}

void A_B_Division (int& A, int& B, double remainder)
{
A%B=remainder
//I'm not sure this will do what i'm trying to do//
A=B
B=remainder
}
}

This is my rough start.. It has some problems but i'm kinda confused as to where to go
I'm not sure how to plug the remainder in to a variable and i'm not sure my loops are right..
can someone give me some pointers?

Recommended Answers

All 4 Replies

This looks like the formula for a recursive procedure, which is a procedure that calls itself. You have to be careful with recursive procedures because you risk going into an endless (until you run out of memory) loop -- unless you make sure you have an exit condition -- your exit co ndition is when A/B = 0. In this case, your procedure returns B. A recursive procedure generally follows this form:

int recursiveFunction(int x, int y)
{
  if (exit criteria is met)
  {
    return a parameter;
  }
  else
 {
     manipulate the parameters in such a way that the exit criteria will eventually be met;
     return recursiveFunction(x,y);
  }
}

See if that can get you going in the right direction. Remember your semicolons and which side of the equal sign variables belong when being assigned.

ok this is my new attempt but it gives me a fatal error

#include <iostream>
#include <cmath> 
using namespace std;

int A_B_Division(int, int, double);

int main()
	{
	int A, B;
	double remainder;

	cout<<"Please enter two integers"<<endl;
	cin>>A>>B;

	remainder=A%B;

	cout<<"The integers "<<A<<" and "<<B<<" have the greatest common divisor ";
	A_B_Division(A, B, remainder);
	}

int A_B_Division (int A, int B, double remainder)
	{
	if (remainder==0)
		{
		cout<<B<<endl;
		return B;
		}
		
	else
		{
		B=A;
		remainder=B;
		}

	}

someone please, i'm at a total impass

I wouldn't include the remainder variable in main. You don't need to pass it into your function. I would make remainder an int and assign it in your div function. Instead of just calling your function in main, you want to output its return value. At the end of your else block, you need to make your recursive call: return A_B_Division(A, B); , otherwise your code goes nowhere from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.