## \$show=/label

Different ways to find the LCM in java using for loop and if statements. And also example to calculate LCM using GCD.

## 1. Overview

In this short article, you'll learn how to find the LCM of two numbers in java by using simple while loop and by using GCD of same numbers.

It is easy to understand if you know the below topics.

If else condition

while loop

LCM means least common multiple - In other words a minimum number that is divisible by given two numbers without any remainder.

Example

Input : a = 12, b = 40

Multiple factors are : 2, 2, 3, 10

LCM is : 2 * 2 * 3 * 10 = 120

## 2. Example To Find LCM Using While Loop in java

In the below example, first declared two int variables a, b.

Next, take the highest value from a and b as lcm.

Finally, run the while loop until both numbers are divided by the same number.

```public class LCMWhileLoopExample {

public static void main(String[] args) {

// creating a two integer variables
int a = 100;
int b = 152;

// Example 1
printLCM(a, b);

// Example 2
a = 100;
b = 200;

printLCM(a, b);

// Example 3
a = 82;
b = 205;

printLCM(a, b);

}

private static void printLCM(int a, int b) {
// taking big number as lcm default.

int lcm = b;
if (a > b) {
lcm = a;
}

while (true) {

if (lcm % a == 0 && lcm % b == 0) {
System.out.printf("\nThe LCM of two numbers %d and %d is %d.", a, b, lcm);
break;
}

lcm++;
}

}

}

```

Output:

```The LCM of two numbers 100 and 152 is 3800.
The LCM of two numbers 100 and 200 is 200.
The LCM of two numbers 82 and 205 is 410.
```

## 3. Example To Find LCM Using GCD of two numbers in java

This is the different approach and takes less time for computing the LCM.

But, first need to calculate the GCD of two numbers.

GCD means Greatest Common Divisor means highest number that is divided by two numbers without any remainder.

If you do not know GCD, you can learn how to find GCD of two numbers?

After finding the GCD, use the below formula to get LCM.

LCM = a * b / GCD.

```public class LCMFromGCDExample {

public static void main(String[] args) {

printLCMUsingGCD(100, 152);
printLCMUsingGCD(10, 1100);
printLCMUsingGCD(140, 300);

}

private static void printLCMUsingGCD(int a, int b) {

// finding GCD
int gcd = 1;
for (int i = 1; i <= a && i <= b; i++) {

if(a % i == 0 && b % i == 0) {
gcd = i;
}
}

int lcm = a * b / gcd;
System.out.printf("\nThe LCM of two numbers %d and %d is %d.", a, b, lcm);

}

}
```

Output

```The LCM of two numbers 100 and 152 is 3800.
The LCM of two numbers 10 and 1100 is 1100.
The LCM of two numbers 140 and 300 is 2100.
```

## 4. Conclusion

In this article, you've seen how to calculate the LCM of two numbers using while loop and  by using GCD method.

GitHub LCM While Loop

GitHub LCM using GCD

In next article, How to find the LCM for large numbers ?

BLOGGER

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,23,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,84,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,12,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,10,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,77,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,93,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,23,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Finding LCM In 2 Ways in Java With Simple Example Programs
Finding LCM In 2 Ways in Java With Simple Example Programs
Different ways to find the LCM in java using for loop and if statements. And also example to calculate LCM using GCD.
JavaProgramTo.com
https://www.javaprogramto.com/2020/11/java-program-find-lcm.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/11/java-program-find-lcm.html
true
3124782013468838591
UTF-8