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

Example:

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.

To do this we will use the loop through the array and use 2 numbers at a time to find the lcm of the array.

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.

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

Let’s do the same in Java.

Output

If you have any doubts or suggestion then comment below.

Leave a Reply

Close Menu