Find gcd and lcm of two numbers?
public int GCD(int a, int b) {
Follow on:
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
public int LCM(int a, int b) {
return (a / GCD(a, b)) * b;
Explanation:
GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b).