Learn Java by Example: Java Program to Find LCM of two Numbers

Java Program to Find LCM of two Numbers

In this program, you’ll learn to find the lcm of two number by using GCD, and by not using GCD. This is done using for and while loops in Java.

 


The LCM of two integers is the smallest positive integer that is perfectly divisible by both the numbers (without a remainder).

Example 1: LCM using while Loop and if Statement


public class Main{
  public static void main(String[] args){

    int n1 = 72, n2 = 120, lcm;

    // maximum number between n1 and n2 is stored in lcm
    lcm = (n1 > n2) ? n1 : n2;

    // Always true
    while(true) {
      if( lcm % n1 == 0 && lcm % n2 == 0 ) {
        System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
        break;
      }
      ++lcm;
    }
  }
}

Output

The LCM of 72 and 120 is 360.

In this program, the two numbers whose LCM is to be found are stored in variables n1 and n2 respectively.

Then, we initially set lcm to the largest of the two numbers. This is because, LCM cannot be less than the largest number.

Inside the infinite while loop (while(true)), we check if lcm perfectly divides both n1 and n2 or not.

If it does, we’ve found the LCM. We print the LCM and break out from the while loop using break statement.

Else, we increment lcm by 1 and re-test the divisibility condition.


We can also use GCD to find the LCM of two numbers using the following formula:

LCM = (n1 * n2) / GCD

If you don’t know how to calculate GCD in Java, check Java Program to find GCD of two numbers.

Example 2: Calculate LCM using GCD


public class Main{
  public static void main(String[] args){

    int n1 = 72, n2 = 120, gcd = 1;

    for(int i = 1; i <= n1 && i <= n2; ++i) {
      // Checks if i is factor of both integers
      if(n1 % i == 0 && n2 % i == 0)
        gcd = i;
    }

    int lcm = (n1 * n2) / gcd;
    System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
  }
}

The output of this program is the same as Example 1.

Here, inside the for loop, we calculate the GCD of the two numbers – n1 and n2. After the calculation, we use the above formula to calculate the LCM.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.