LCM of Two Numbers in Java17 Mar 2025 | 4 min read In arithmetic, the Least Common Multiple (LCM) of two or more numbers is the least positive number that can be divided by both the numbers, without leaving the remainder. It is also known as Lowest Common Multiple (LCM), Least Common Denominator, and Smallest Common Multiple. It is denoted by LCM (a, b) or lcm (a, b) where a and b are two integers. It is used when we add, subtract, or compare the fractions. While we perform addition or subtraction of the fractions, we find the LCM of the denominators and then solve the fractions. The LCM of the denominators is known as the Least Common Denominator (LCD). Properties of LCM
![]() How to Find LCM?There are four methods to find the LCM, are as follows:
Using the Greatest Common Divisor (GCD) MethodWe can also calculate the LCM by using the GCD. The formula for LCM using the GCD is: ![]() Greatest Common Divisor: It is the highest number that completely divides two or more numbers. It is abbreviated for GCD. It is also known as the Greatest Common Factor (GCF) or the Highest Common Factor (HCF). How to Find GCD? In order to find the GCD follow the steps given below:
Let's understand it through examples. Example: Find the LCM of 8 and 10. Solution: According to the formula that we have learned above: ![]() First, we find the GCD of 8 and 10. Factors of 8: 1, 2, 4, 8 Factors of 10: 1, 2, 5, 10 Common Factors: 1, 2 Greatest Common Divisor: 2 ![]() Hence, the LCM of 8 and 10 is 40. Let's implement the logic in a Java program. Using if Statement LcmExample1.java Output: The LCM of 12 and 9 is 36. Using Recursion LcmExample2.java Output: Enter the first number: 45 Enter the second number: 78 LCM of 45 and 78 is 1170 Using Multiples of NumbersIt is a very lengthy method, so it is not usually used. Follow the steps given below to find the LCM using multiples of numbers.
Let's understand it through examples. Example: What is the LCM of 9 and 12. Solution: Multiples of 9: 9, 18, 27, 36 Multiples of 12: 12, 24, 36 LCM (9,12)=36 because it is a common multiple in both. Hence, the LCM of 9 and 12 is 36. Let's implement the logic in a Java program. LcmExample4.java Output: Enter the first number: 123 Enter the second number: 213 LCM of 123 and 213 is: 8733 Next TopicHow to Take Array Input in Java |
Mutable Class in Java
In the world of object-oriented programming, the concept of immutability is often emphasized for its benefits in terms of code stability and predictability. However, there are situations where mutable classes play a crucial role, providing flexibility and the ability to modify object state. In Java, a...
4 min read
LinkedTransferQueue tryTransfer() method in Java with Examples
In Java, LinkedTransferQueue is a concurrent queue implementation which possesses the combination of characteristics from both the traditional blocking queues and the direct handoff queues. It implements the TransferQueue interface that is extending the BlockingQueue class and extends the functionalities of the producer-consumer scenario through...
14 min read
Dangling Else Problem in Java
The dangling else problem is an ambiguity of language interpretation. In programming, we can write conditionally executed code in the following two forms: if-then form if-then-else form The problem rarely occurs while we deal with the nested if-else statement. It is an ambiguity in which it is not clear...
2 min read
Predicate.not() Method in Java with Examples
Java 11 introduces a tool called Predicate.not() to make negating predicates easier. Predicates, which are frequently employed in filtering and conditional logic, are functional interfaces that express boolean-valued functions of a single argument. Negating a predicate in Java 11 required a significantly lengthier method. Predicate.not()...
4 min read
Vertical zig-zag traversal of a tree in Java
The goal is to get the outcome of the items in the Vertical Zig-Zag traversal order given a Binary Tree. A tree's vertical zigzag traversal is described as follows: List the first level's elements in the correct order from right to left; if no parts remain, move...
6 min read
Grepcode java.util.Date
What does the java.util.Date Class does? The java.util.Date class in Java provides the date and time. It might be advantageous if we imported java.util. If we want to implement these classes in our code, use Java.util.Date class. The constructors as well as methods offered by this class allow...
5 min read
AtomicLongArray set() method in Java with Examples
A built-in Java method that allows you to set a value at any location in the AtomicLongArray is called Java.util.concurrent.atomic.AtomicLongArray.set(). This function modifies the value at that index by accepting as an argument the AtomicLongArray's index value. There is absolutely nothing that this method returns....
3 min read
Doubly Linked List Program in Java
Doubly linked list programs are very complex programs to understand because the node of the doubly linked list contains two fields, ious and . In C and C++, it is very easy to maintain a doubly linked list using pointers, but in Java, there is no...
13 min read
Java Program for Maximum Product Subarray
In the field of computer programming, the maximum product subarray problem is a popular challenge that requires finding the contiguous subarray within an array of integers that has the largest product. This problem can be efficiently solved using dynamic programming techniques. In this article, we will...
4 min read
Javac Command Not Found
Javac command is usually used to check whether the Java is installed in our system or not. When the Java is not installed in our system and we try to run Javac command, we get Javac command not found or Java is not Recognized. We can...
2 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India



