Java Program to Find LCM of Two Numbers
LCM (i.e. Least Common Multiple) is the largest of the two stated numbers that can be divided by both the given numbers.
LCM of 15 and 25 is 75, and LCM of 3 and 7 is 21.
Therefore, firstly find all the prime factors of both the stated numbers, then find the union of all those factors that are present in both the given numbers. And at last, return the product of elements which are in union.
Below given formula for finding the LCM of two numbers ‘u’ and ‘v’ gives an efficient solution.
u x v = LCM(u, v) * GCD (u, v) LCM(u, v) = (u x v) / GCD(u, v)
Note: GCD is the greatest common divisor.
LCM of 25 and 15 is 75
Similarly, you can find the LCM of any two given numbers.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.