Skip to content

Instantly share code, notes, and snippets.

@renesansz
Created September 13, 2015 06:38
Show Gist options
  • Save renesansz/141a6c10be0c9d56cc4d to your computer and use it in GitHub Desktop.
Save renesansz/141a6c10be0c9d56cc4d to your computer and use it in GitHub Desktop.
Get LCM of two numbers in Java
/**
* Get the Greatest Common Denominator of two numbers.
*
* @param {int} num1
* @param {int} num2
*
* @return {int}
* */
private static int getGCD(int num1, int num2) {
while (num2 > 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
/**
* Get the Least Common Multiple of two numbers.
*
* @param {int} num1
* @param {int} num2
*
* @return {int}
*/
private static int getLCM(int num1, int num2) {
return num1 * (num2 / getGCD(num1, num2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment