In this tutorial, we will see **“How to calculate LCM (Least Common Multiple) of two numbers using Java?”**

### Calculate LCM of two numbers in Java

You can use the following formula to calculate the least common multiple (LCM) of two numbers in Java:

1 | LCM(a, b) = (a * b) / GCD(a, b) |

Java – How to Calculate LCM of two numbers? (Simplest Example) Click To Tweet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * Calculate LCM of two numbers in Java * @author Deepak Verma * */ import java.math.BigInteger; public class LCM_Two_Numbers_Java_Example { public static void main(String[] args) { int a = 35; int b = 90; int gcd = BigInteger.valueOf(a) .gcd(BigInteger.valueOf(b)) .intValue(); int lcm = (a * b) / gcd; System.out.println("Least Common Multiple(LCM) of "+a+" and "+b+" = "+lcm); } } |

**Output:**

1 | Least Common Multiple(LCM) of 35 and 90 = 630 |

### check if text or string present in a file using java 8

In the above example, At first, we use the GCD method to get the gcd of two numbers then we can use the above formula to calculate the LCM on the gcd value.

### Calculate LCM of two numbers in Java

###### skip method java 8 stream

Java – How to Calculate LCM of two numbers? (Simplest Example) Click To Tweet

**Do you like this Post? – then check my other helpful posts:**

- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8

**Other Useful References:**