Created
August 11, 2016 09:04
-
-
Save nichtemna/01e98ee9728395530ed9d1dfbe6e7399 to your computer and use it in GitHub Desktop.
Least common multiple of two numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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