Last Updated: 20 April, 2023
On this tutorial page we are going to learn what is LCM and how to find LCM of two numbers in Java.
The Least Common Multiple (LCM) of two or more numbers is the smallest number that is a multiple of all the given numbers. There are different methods to find the LCM of two or more numbers, but the most common method is the prime factorization method. Here are the steps to find the LCM of two or more numbers using prime factorization:
Step 1: Find the prime factorization of each number. To do this, you can divide the number by its smallest prime factor, then divide the result by its smallest prime factor, and so on until the result is 1. Write down the prime factors and their exponents for each number.
Step 2: Identify all the prime factors involved in the prime factorizations of the numbers. Write down each prime factor once, and write down the highest exponent of that factor among the numbers.
Step 3: Multiply the prime factors identified in step 2, each raised to its highest exponent.
The result is the LCM of the given numbers.
Here is an example to find the LCM of 12 and 18 using prime factorization:
Step 1: Prime factorization of 12: 12 = 2 * 2 * 3
Prime factorization of 18: 18 = 2 * 3 * 3
Step 2: The prime factors involved in the prime factorizations of 12 and 18 are 2 and 3. The highest exponent of 2 is 2 (from 12), and the highest exponent of 3 is 2 (from 18).
Step 3: LCM = 2^2 * 3^2 = 36
Therefore, the LCM of 12 and 18 is 36.
Note that this method can be extended to find the LCM of more than two numbers by including all the prime factors involved in the prime factorizations of the numbers, and writing down the highest exponent of each factor among the numbers.
Example: Find LCM of two numbers
Output
The LCM of 12 and 18 is 36.
That's all guys, hope this Java Program is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com