Java Program to Find LCM of Two Numbers Last Updated : 22 Apr, 2024 Comments Improve Suggest changes 19 Likes Like Report Try it on GfG Practice LCM (i.e. Least Common Multiple) is the largest of the two stated numbers that can be divided by both the given numbers. In this article, we will write a program to find the LCM in Java Java Program to Find the LCM of Two NumbersThe easiest approach for finding the LCM is to Check the factors and then find the Union of all factors to get the result. Below is the implementation of the above method: Java // Java Program to find // the LCM of two numbers import java.io.*; // Driver Class class GFG { // main function public static void main(String[] args) { // Numbers int a = 15, b = 25; // Checking for the largest // Number between them int ans = (a > b) ? a : b; // Checking for a smallest number that // can de divided by both numbers while (true) { if (ans % a == 0 && ans % b == 0) break; ans++; } // Printing the Result System.out.println("LCM of " + a + " and " + b + " : " + ans); } } OutputLCM of 15 and 25 : 75Using Greatest Common DivisorBelow 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)Here, GCD is the greatest common divisor. Below is the implementation of the above method: Java // Java program to find LCM // of two numbers. class gfg { // Gcd of u and v // using recursive method static int GCD(int u, int v) { if (u == 0) return v; return GCD(v % u, u); } // LCM of two numbers static int LCM(int u, int v) { return (u / GCD(u, v)) * v; } // main method public static void main(String[] args) { int u = 25, v = 15; System.out.println("LCM of " + u + " and " + v + " is " + LCM(u, v)); } } OutputLCM of 25 and 15 is 75Complexity of the above method:Time Complexity: O(log(min(a,b))Auxiliary Space: O(log(min(a,b)) Create Quiz LCM of 2 Numbers Visit Course Comment K Kanchan_Ray Follow 19 Improve K Kanchan_Ray Follow 19 Improve Article Tags : Java Java Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like