Created
September 13, 2015 06:38
-
-
Save renesansz/141a6c10be0c9d56cc4d to your computer and use it in GitHub Desktop.
Get LCM of two numbers in Java
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
/** | |
* 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