Program to find LCM of two numbers

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 product of elements in union.



An efficient solution is based on 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 above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find LCM of two numbers 
#include <iostream> 
using namespace std;
// Recursive function to return gcd of a and b 
class gfg
{
 public : int gcd(int a, int b) 
 
    // Everything divides 0 
    if (a == 0 || b == 0) 
        return 0; 
   
    // Base case    
    if (a == b) 
        return a; 
   
    // a is greater 
    if (a > b) 
        return gcd(a-b, b); 
    return gcd(a, b-a); 
 }
  
   
// Function to return LCM of two numbers 
  int lcm(int a, int b) 
 
    return (a*b)/gcd(a, b); 
 
} ;
// Driver program to test above function 
int main() 
    gfg g;
    int a = 15, b = 20; 
    cout<<"LCM of "<<a<<" and "<<b<<" is "<<g.lcm(a, b); 
    return 0; 

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 
    // Everything divides 0 
    if (a == 0 || b == 0) 
        return 0; 
  
    // Base case    
    if (a == b) 
        return a; 
  
    // a is greater 
    if (a > b) 
        return gcd(a-b, b); 
    return gcd(a, b-a); 
  
// Function to return LCM of two numbers 
int lcm(int a, int b) 
    return (a*b)/gcd(a, 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; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    {
        // Everything divides 0 
        if (a == 0 || b == 0)
           return 0;
       
        // base case
        if (a == b)
            return a;
       
        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
      
    // method to return LCM of two numbers
    static int lcm(int a, int b)
    {
        return (a*b)/gcd(a, 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find LCM of two numbers
  
# Recursive function to return gcd of a and b
def gcd(a,b):
  
    # base case if a and b are equal 
    if (a == b):
        return a
    # if a is greater
    if (a > b):
        return gcd(a-b, b)
          
    return gcd(a, b-a)
  
# Function to return LCM of two numbers
def lcm(a,b):
    return (a*b) / gcd(a,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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    {
          
        // Everything divides 0 
        if (a == 0 || b == 0)
        return 0;
      
        // base case
        if (a == b)
            return a;
      
        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
      
    // method to return 
    // LCM of two numbers
    static int lcm(int a, int b)
    {
        return (a * b) / gcd(a, 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find LCM of two numbers
  
// Recursive function to 
// return gcd of a and b
function gcd( $a, $b)
{
      
    // base case
    if ($a == $b)
        return $a;
  
    // a is greater
    if ($a > $b)
        return gcd($a - $b, $b);
    return gcd($a, $b - $a);
}
  
// Function to return LCM
// of two numbers
function lcm( $a, $b)
{
    return ($a * $b) / gcd($a, $b);
}
  
    // Driver Code
    $a = 15; 
    $b = 20;
    echo "LCM of ",$a, " and "
         ,$b, " is ", lcm($a, $b);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output:

LCM of 15 and 20 is 60

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : vt_m, SoM15242