Write a java program to find the lcm of array i.e more than 2 numbers.


Recommended: GCD of Array

To find the lcm of array we will use gcd because:

For 2 number LCM = (n1*n2)/GCD.

But this is not valid for more than 2 numbers i.e LCM != (n1*n2*n3)/GCD.

Thus to find lcm of the whole array we will have to deal with two numbers at a time and use its result for the next number.

Suppose LCM of the first two numbers of the array (i.e array[0] and array[1]) is lcm1, for next iteration we will find the LCM of lcm1 and array[2] as lcm2, then for 3rd iteration LCM of lcm2 and array[3] would be lcm3 and so on.

The last LCM value i.e the LCM of lcm(n-1) and arra[n] would be the LCM of the whole array.

For this, we will loop through the array and use Lcm of previous two numbers * array[n] / GCD to find the lcm of the array.

Let’s see how we can do it in Java.


If you have any doubts or suggestion then comment below.

Leave a Reply

ten + 15 =