Open In App

Java Program to Find LCM of Two Numbers

Last Updated : 22 Apr, 2024
Comments
Improve
Suggest changes
16 Likes
Like
Report

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

lcm in java

Java Program to Find the LCM of Two Numbers

The 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:


Output
LCM of 15 and 25 : 75

Using Greatest Common Divisor

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)

Here, GCD is the greatest common divisor.

Below is the implementation of the above method:


Output
LCM of 25 and 15 is 75

Complexity of the above method:

Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))


LCM of 2 Numbers
Visit Course explore course icon
Next Article
Article Tags :
Practice Tags :

Similar Reads