Program to find LCM of two numbers

22 Jun, 2022
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’. 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
   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
Java
Python3
C#
PHP
Javascript
// C++ program to find LCM of two numbers#include <iostream>using namespace std; // Recursive function to return gcd of a and blong 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 numberslong long lcm(int a, int b){    return (a / gcd(a, b)) * b;}  // Driver program to test above functionint main(){    int a = 15, b = 20;    cout <<"LCM of " << a << " and "         << b << " is " << lcm(a, b);    return 0;}
// C program to find LCM of two numbers#include <stdio.h> // Recursive function to return gcd of a and bint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to return LCM of two numbersint lcm(int a, int b){    return (a / gcd(a, b)) * b;} // Driver program to test above functionint main(){    int a = 15, b = 20;    printf("LCM of %d and %d is %d ", a, b, lcm(a, b));    return 0;}
// Java program to find LCM of two numbers.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));    }}
# Python program to find LCM of two numbers # Recursive function to return gcd of a and bdef gcd(a,b):    if a == 0:        return b    return gcd(b % a, a) # Function to return LCM of two numbersdef lcm(a,b):    return (a / gcd(a,b))* b # Driver program to test above functiona = 15b = 20print('LCM of', a, 'and', b, 'is', lcm(a, b)) # This code is contributed by Danish Raza
// 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.
<?php// PHP program to find LCM of two numbers // Recursive function to// return gcd of a and bfunction gcd( $a, $b){   if ($a == 0)        return $b;    return gcd($b % $a, $a);} // Function to return LCM// of two numbersfunction 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.?>
<script> // Javascript program to find LCM of two numbers // Recursive function to return gcd of a and bfunction gcd(a, b){if (b == 0)    return a;return gcd(b, a % b);} // Function to return LCM of two numbersfunction 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>
LCM of 15 and 20 is 60
Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))
https://youtu.be/anSfYgbo694
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
vt_m
SoumikMondal
DeepakMangla1
jayonet19
mayanktyagi1709
mayur_patil
mk420464
subhammahato348
souravmahato348
codewithrathi
GCD-LCM
SAP Labs
Mathematical
School Programming
SAP Labs
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java