Skip to content

Instantly share code, notes, and snippets.

@nichtemna
Created August 11, 2016 09:04
Show Gist options
  • Save nichtemna/592b2002fabac45eb0697a0e96de26b7 to your computer and use it in GitHub Desktop.
Save nichtemna/592b2002fabac45eb0697a0e96de26b7 to your computer and use it in GitHub Desktop.
Least common multiple of two numbers
public class LCM {
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
int first = scaner.nextInt();
int second = scaner.nextInt();
int gcd = getGCD(first, second);
long lcm = getLCM(first, second, gcd);
System.out.println(lcm);
}
private static long getLCM(int first, int second, int gcd) {
return ((long)first * (long)second) / gcd;
}
private static int getGCD(int first, int second) {
while (second != 0) {
int prevSecond = second;
second = first % second;
first = prevSecond;
}
return first;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment