C++ to find the greatest common divisor and the least common multiple

  • 2020-05-19 05:15:18
  • OfStack

C++ to find the greatest common divisor and the least common multiple

The greatest common divisor

Division:


int maxDivisor(int a, int b) 
{ 
  int c = b; 
  while (a%b != 0) 
  { 
    c = a%b; 
    a = b; 
    b = c; 
  } 
  return c; 
} 

Phase subtraction:


int maxDivisor(int a, int b) 
{ 
  while (a != b) 
  { 
    if (a>b) a = a - b; 
    else b = b - a; 
  } 
  return a; 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: