The Wayback Machine - https://web.archive.org/web/20240703214229/https://www.geeksforgeeks.org/program-to-find-lcm-of-two-numbers/
Open In App

Program to find LCM of two numbers

Last Updated : 02 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers. 

For example, LCM of 15 and 20 is 60, and LCM of 5 and 7 is 35.

A simple solution is to find all prime factors of both numbers, then find union of all factors present in both numbers. Finally, return the product of elements in union.

An efficient solution is based on the below formula for LCM of two numbers ‘a’ and ‘b’. 

   a x b = LCM(a, b) * GCD (a, b)

LCM(a, b) = (a x b) / GCD(a, b)

We have discussed function to find GCD of two numbers. Using GCD, we can find LCM. 

Below is the implementation of the above idea:

C++
// C++ program to find LCM of two numbers 
#include <iostream> 
using namespace std;

// Recursive function to return gcd of a and b 
long long gcd(long long int a, long long int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// Function to return LCM of two numbers 
long long lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}
 
// Driver program to test above function 
int main() 
{ 
    int a = 15, b = 20; 
    cout <<"LCM of " << a << " and "
         << b << " is " << lcm(a, b); 
    return 0; 
} 
C
// C program to find LCM of two numbers 
#include <stdio.h> 

// Recursive function to return gcd of a and b 
int gcd(int a, int b) 
{ 
    if (a == 0)
        return b; 
    return gcd(b % a, a); 
} 

// Function to return LCM of two numbers 
int lcm(int a, int b) 
{ 
    return (a / gcd(a, b)) * b;
} 

// Driver program to test above function 
int main() 
{ 
    int a = 15, b = 20; 
    printf("LCM of %d and %d is %d ", a, b, lcm(a, b)); 
    return 0; 
} 
Java
// Java program to find LCM of two numbers.
import java.io.*;
public class Test
{
    // Recursive method to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b; 
        return gcd(b % a, a); 
    }
    
    // method to return LCM of two numbers
    static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
    
    // Driver method
    public static void main(String[] args) 
    {
        int a = 15, b = 20;
        System.out.println("LCM of " + a +
                           " and " + b + 
                      " is " + lcm(a, b));
    }
}
Python3
# Python program to find LCM of two numbers

# Recursive function to return gcd of a and b
def gcd(a,b):
    if a == 0:
        return b
    return gcd(b % a, a)

# Function to return LCM of two numbers
def lcm(a,b):
    return (a // gcd(a,b))* b

# Driver program to test above function
a = 15 
b = 20
print('LCM of', a, 'and', b, 'is', lcm(a, b))

# This code is contributed by Danish Raza
C#
// C# program to find LCM
// of two numbers.
using System;
class GFG {
    
    // Recursive method to 
    // return gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b; 
        return gcd(b % a, a); 
    }
    
    // method to return 
    // LCM of two numbers
    static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
    
    // Driver method
    public static void Main() 
    {
        int a = 15, b = 20;
        Console.WriteLine("LCM of " + a +
         " and " + b + " is " + lcm(a, b));
    }
}

// This code is contributed by anuj_67.
Javascript
<script>

// Javascript program to find LCM of two numbers 

// Recursive function to return gcd of a and b 
function gcd(a, b) 
{ 
if (b == 0) 
    return a; 
return gcd(b, a % b); 
} 

// Function to return LCM of two numbers 
function lcm(a, b) 
{ 
    return (a / gcd(a, b)) * b; 
} 

// Driver program to test above function 
 
    let a = 15, b = 20; 
    document.write("LCM of " + a + " and "
    + b + " is " + lcm(a, b)); 
    

// This code is contributed by Mayank Tyagi

</script>
PHP
<?php
// PHP program to find LCM of two numbers

// Recursive function to 
// return gcd of a and b
function gcd( $a, $b)
{
   if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}

// Function to return LCM
// of two numbers
function lcm( $a, $b)
{
    return ($a / gcd($a, $b)) * $b;
}

    // Driver Code
    $a = 15; 
    $b = 20;
    echo "LCM of ",$a, " and "
         ,$b, " is ", lcm($a, $b);

// This code is contributed by anuj_67.
?>

Output
LCM of 15 and 20 is 60

Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))

Another Efficient Approach: Using conditional for loop

C++
// C++ program to find LCM of 2 numbers
#include <bits/stdc++.h>
using namespace std;

// Function to return LCM of two numbers
int LCM(int a, int b)
{
    int greater = max(a, b);
    int smallest = min(a, b);
    for (int i = greater; ; i += greater) {
        if (i % smallest  == 0)
            return i;
    }
}

// Driver program to test above function
int main()
{
    int a = 10, b = 5;
    cout << "LCM of " << a << " and " 
         << b << " is " << LCM(a, b);
    return 0;
}
Java
// Java program to find LCM of 2 numbers

import java.util.Scanner;

public class LCM {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int a = 10;
        int b = 5;
        int lcm = findLCM(a, b);
        System.out.println("LCM of " + a + " and " + b
                           + " is " + lcm);
        sc.close();
    }
    // Function to return LCM of two numbers
    public static int findLCM(int a, int b)
    {
        int greater = Math.max(a, b);
        int smallest = Math.min(a, b);
        for (int i = greater;; i += greater) {
            if (i % smallest == 0)
                return i;
        }
    }
}
Python
# Python program to find LCM of two numbers

# Function to return LCM of two numbers
def LCM(a, b):
    greater = max(a, b)
    smallest = min(a, b)
    for i in range(greater, a*b+1, greater):
        if i % smallest == 0:
            return i

# Driver program to test above function
if __name__ == '__main__':
    a = 10
    b = 5
    print("LCM of", a, "and", b, "is", LCM(a, b))
C#
// C# program to find LCM of 2 numbers
using System;

class LCMProgram 
{

  // Function to return LCM of two numbers
  static int LCM(int a, int b)
  {
    int greater = Math.Max(a, b);
    int smallest = Math.Min(a, b);
    for (int i = greater;; i += greater) {
      if (i % smallest == 0)
        return i;
    }
  }

  // Driver program to test above function
  static void Main()
  {
    int a = 10, b = 5;
    Console.WriteLine("LCM of " + a + " and " + b
                      + " is " + LCM(a, b));
  }
}
Javascript
// Javascript program to find LCM of two numbers

// Function to return LCM of two numbers
function LCM(a, b){
    let greater = Math.max(a, b);
    let smallest = Math.min(a, b);
    for(let i = greater; i <= a*b; i+=greater){
        if(i % smallest == 0){
            return i;
        }
    }
}

// Driver program to test above function
let a = 10;
let b = 5;
console.log("LCM of", a, "and", b, "is", LCM(a, b));

Output
LCM of 10 and 5 is 10

Time Complexity: O(min(a,b))
Auxiliary Space: O(1)




Previous Article
Next Article

Similar Reads

Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
Given an integer n, the task is to find the sum: LCM(1, n) + LCM(2, n) + LCM(3, n) + ... + LCM(n, n) where LCM(i, n) is the Least Common Multiple of i and n. Examples: Input: 3 Output: 10 LCM(1, 3) + LCM(2, 3) + LCM(3, 3) = 3 + 6 + 3 = 12 Input: 5 Output: 55 LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5) = 55 Naive Approach: LCM of two n
11 min read
Find triplet (A, B, C) such that LCM(A, B) + LCM(A, C) + LCM(B, C) equals N
Given an integer N. Find a triplet let say {A, B, C} such that LCM(A, B) + LCM(B, C) + LCM(A, C) = N. If no possible triplets are there, output -1 instead. Examples: Input: N = 1Output: -1Explanation: No possible triplets are there. Input: N = 15Output: 5 5 5Explanation: LCM(5, 5) + LCM(5, 1) + LCM(5, 1) = 5 + 5 + 5 = 15. Observations: Observation
8 min read
Minimum replacement of pairs by their LCM required to reduce given array to its LCM
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of pairs (arr[i], arr[j]) from the given array needed to be replaced with their LCM such that the array is reduced to a single element equal to the LCM of the initial array.Examples: Input: arr[] = {1, 2, 3, 4} Output: 3 Explanation: LCM of the array = 12
9 min read
Program to find the LCM of two prime numbers
Given two prime numbers N and M, the task is to find the Least Common Multiple(LCM) of the two given prime numbers.Examples: Input: N = 3, M = 7 Output: 21 Explanation: The least numbers greater than equals to 3 and 7 which is a multiple of 3 and 7 is 21. Input: N = 5, M = 5 Output: 5 Explanation: The least numbers greater than equals to 5 and 5 wh
4 min read
C Program to find LCM of two numbers using Recursion
Given two integers N and M, the task is to find their LCM using recursion. Examples: Input: N = 2, M = 4Output: 4Explanation: LCM of 2, 4 is 4. Input: N = 3, M = 5Output: 15Explanation: LCM of 3, 5 is 15. Approach: The idea is to use the basic elementary method of finding LCM of two numbers. Follow the steps below to solve the problem: Define a rec
3 min read
Find two distinct numbers such that their LCM lies in given range
Given two numbers L and R, the task is to find two distinct minimum positive integers X and Y such that whose LCM lies in the range [L, R]. If there doesn't exist any value of X and Y then print "-1". Examples: Input: L = 3, R = 8 Output: x = 3, y=6Explanation:LCM of 3 and 6 is 6 which is in range 3, 8 Input: L = 88, R = 90Output: -1Explanation:Min
4 min read
Find two numbers with given sum and maximum possible LCM
Given an integer X, the task is to find two integers A and B such that sum of these two numbers is X and the LCM of A and B is maximum. Examples: Input: X = 15Output: 7 8Explanation: 7 + 8 = 15 and LCM(7, 8) = 56 is the maximum possible. Input: X = 30 Output: 13 17Explanation:13 + 17 = 30 and LCM(13, 17) = 221 is the maximum possible. Naive Approac
10 min read
Program to find LCM of 2 numbers without using GCD
Finding LCM using GCD is explained here but here the task is to find LCM without first calculating GCD.Examples: Input: 7, 5 Output: 35 Input: 2, 6 Output: 6 The approach is to start with the largest of the 2 numbers and keep incrementing the larger number by itself till smaller number perfectly divides the resultant. C/C++ Code // C++ program to f
4 min read
Maximum sum of distinct numbers such that LCM of these numbers is N
Given a positive number N. The task is to find the maximum sum of distinct numbers such that the LCM of all these numbers is equal to N. Examples: Input : 2 Output : 3 The distinct numbers you can have are just 1 and 2 and their sum is equal to 3. Input : 5 Output : 6Recommended PracticeMaximum Sum LCMTry It! As the LCM of all the numbers is N. So
4 min read
LCM of two large numbers
Given two large numbers 'a' and 'b' such that(10^20&lt;=a, b&lt;=10^300). Find the LCM of two large numbers given. Examples: Input: a = 234516789234023485693020129 b = 176892058718950472893785940 Output: 41484157651764614525905399263631111992263435437186260 Input: a = 36594652830916364940473625749407 b = 448507083624364748494746353648484939 Output:
3 min read
three90RightbarBannerImg