SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size

How to find greatest common divisor (GCD) of two numbers in C++?

E-mail Print PDF
The below code is a function in C++ use to return GCD (greatest common divisor of two numbers.
Example:
GCD of 20 and 5 is 5
GCD of 24 and 18 is 2
int GCD(int x, int y)
{
	while(x!=y)
	{
		if(x>y)x=x-y;
		else y=y-x;
	}
	return(x);
}