LCM of two Numbers in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

In mathematical terms, the smallest possible integer that is divisible by two (or more) numbers is known as the Least Common Multiple (LCM) of the numbers. We will be discussing various methods to calculate the LCM of the numbers.

Introduction

The Least Common Multiple (LCM) of two or more numbers is the smallest positive number that can be divided by the numbers without leaving a remainder. LCM is also known by the terms: Lowest Common Multiple (LCM), Least Common Denominator, and Smallest Common Multiple. LCM (a, b) or lcm (a, b) are the symbols for it, where a and b are two numbers.

For example: LCM of 44, 55 and 66 is 6060. Since 6060 is the smallest integer that is completely divisible by all three numbers.

Sharpen Your Fundamentals with Free Learning

Properties of LCM

LCM is associative, commutative and distributive, which means:

  • Associative: LCM (a, b) = LCM (b, a)
  • Commutative: LCM (a, b, c) = LCM (LCM (a, b), c) = LCM (a, LCM (b, c))
  • Distributive: LCM (ka, kb, kc) = kLCM (a, b, c)

Now, that we know about LCM and its properties, let's now look at the different ways to find the LCM of two numbers.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

LCM Using Prime Factorization Method

Our first approach involves finding the prime factors of each number and then calculating the LCM using those, the same way we used to do it in our earlier days at school. Below is the recursive code for the method:

Output:

LCM Using While loop and if Statement

The next method is by using the while loop and if statement. The method is pretty straightforward. Let's dive into the code and understand how it works:

Output:

In the above code, we have initialized the LCM to the greater number and kept incrementing it, until a number is found which is divisible by both numbers.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

LCM Using Multiples of Numbers

The method of finding LCM using multiples of numbers is probably the least used approach. This is mainly because the method is very lengthy. This approach is carried out in two steps:

  1. List each number's multiples until the first common multiple is discovered.
  2. Choose the smallest multiple that all of the numbers have in common.

That's about it. Now, let's code it out:

Output:

Calculate LCM using GCD

Before discussing the method of calculating LCM using GCD, we should understand what GCD is: Greatest Common Divisor: It's the largest number that splits two or more numbers fully. It's also known as the Highest Common Factor (HCF) or the Greatest Common Factor (GCF).

There is also a relationship between GCD and LCM:

Relation between GCD and LCM

Using the above relation, we can easily find the LCM using the GCD. We can now look at the code for the same:

Output:

Note: We can further optimize the time complexity of the code by improvising the time complexity to calculate GCD. To learn more about it, you can refer to GCD of Two Numbers in Java

Conclusion

Finally, let's summarise everything we have learned in the article:

  • The smallest positive integer that is perfectly divisible by two numbers, a and b is the Least Common Multiple (LCM) of two non-zero integers (a, b).
  • LCM is associative, commutative and distributive.
  • LCM of two numbers can be calculated using the methods of prime factorization, while loop and if statement, multiples of numbers, and the GCD of the two numbers.