Flutter Least Common Multiple (LCM)

For finding LCM, we need to use the gcd function that Dart provides :
 


int gcd(int other);

LCM can be calculated as follows:


int getLCM(int a, int b) {
  return (a * b) ~/ gcd(a, b);
}

Here is the complete code :

void main() {
  int a = 10, b = 25;
  int lcm = getLCM(a, b);
  print('LCM of $a and $b is $lcm');
}

int getLCM(int a, int b) {
  return (a * b) ~/ gcd(a, b);
}

int gcd(int a, int b) {
  return a.gcd(b);
}

Output :

LCM of 10 and 25 is 50

© 2020, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags