LCM of three numbers

LCM of three Numbers in Java

Here, in this page we will discuss the program to find lcm of three numbers in Java. We will discuss different approaches in this page.
LCM of three Numbers in Java

Method Discussed :

  • Method 1 : Without finding GCD.
  • Method 2 : Using GCD approach.
  • Method 3 : Recursive approach

Let’s discuss above three methods in brief,

Method 1 :

Store the three elements in the array.

  • Set result = 1
  • Find a common factors of two or more array elements.
  • Multiply the result by common factor and divide all the array elements by this common factor.
  • Repeat steps 2 and 3 while there is a common factor of two or more elements.
  • Multiply the result by reduced (or divided) array elements.

Method 1 : Code in Java

Run
import java.util.Vector;
 
class Main {
 
    static long LCM(int arr[], int n) {
        int max_num = 0;
        for (int i = 0; i < n; i++) {
            if (max_num < arr[i]) {
                max_num = arr[i];
            }
        }
 
        long res = 1;
 
        int x = 2; 
        while (x <= max_num) {
            Vector indexes = new Vector<>();
            for (int j = 0; j < n; j++) {
                if (arr[j] % x == 0) {
                    indexes.add(indexes.size(), j);
                }
            }
 
             if (indexes.size() >= 2) {
                for (int j = 0; j < indexes.size(); j++) {
                    arr[indexes.get(j)] = arr[indexes.get(j)] / x;
                }
 
                res = res * x;
            } 
            else {
                x++;
            }
        }
 
        for (int i = 0; i < n; i++) {
            res = res * arr[i];
        }
 
        return res;
    }
 
    public static void main(String[] args) {
        int arr[] = {7, 35, 21};
        int n = arr.length;
        System.out.println(LCM(arr, n));
    }
}

Output

105

Method 2 :

In this method we will find the LCM of given three numbers by finding their GCD first.Let’s see how the code will work.

Method 2 : Code in Java

Run
public class Main {
     
    public static long lcm_of_array_elements(int[] element_array)
    {
        long lcm_of_array_elements = 1;
        int divisor = 2;
         
        while (true) {
            int counter = 0;
            boolean divisible = false;
             
            for (int i = 0; i < element_array.length; i++) {
 
                
                if (element_array[i] == 0) {
                    return 0;
                }
                else if (element_array[i] < 0) {
                    element_array[i] = element_array[i] * (-1);
                }
                if (element_array[i] == 1) {
                    counter++;
                }
 
                if (element_array[i] % divisor == 0) {
                    divisible = true;
                    element_array[i] = element_array[i] / divisor;
                }
            }
 
            if (divisible) {
                lcm_of_array_elements = lcm_of_array_elements * divisor;
            }
            else {
                divisor++;
            }
 
            if (counter == element_array.length) {
                return lcm_of_array_elements;
            }
        }
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int[] element_array = { 35, 7, 21 };
        System.out.println(lcm_of_array_elements(element_array));
    }
}

Output

105

Method 3 :

In this method we will find the LCM of given three numbers by finding their GCD first in recursive way.Let’s see how the code will work.

Method 3 : Code in Java

Run
import java.util.*;
class Main
{
 
  static int __gcd(int a, int b) 
  { 
    return b == 0? a:__gcd(b, a % b);    
  }
 
  static int LcmOfArray(int[] arr, int idx)
  {
 
    if (idx == arr.length - 1){
      return arr[idx];
    }
    int a = arr[idx];
    int b = LcmOfArray(arr, idx+1);
    return (a*b/__gcd(a,b)); 
  }
 
 
  public static void main(String[] args)
  {
 
    int[] arr = {7, 21, 35};
    System.out.print(LcmOfArray(arr, 0));
  }
}

Output

105