Program to find LCM of two numbers (original) (raw)

Last Updated : 26 Aug, 2025

Given two positive integers **a and **b. Find the Least Common Multiple (LCM) of **a and **b.
**LCM of two numbers is the smallest number which can be divided by both numbers.

**Input : a = 10, b = 5
**Output : 10
**Explanation : 10 is the smallest number divisible by both 10 and 5

**Input : a = 5, b = 11
**Output : 55
**Explanation : 55 is the smallest number divisible by both 5 and 11

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Using Conditional Loop

This approach to calculating the Least Common Multiple (LCM) involves starting from the greater of the two numbers and checking if it's divisible by the smaller number. It iterates through multiples of the larger number, incrementing by the larger number itself in each step. The first multiple that is divisible by the smaller number is the LCM. This method is simple and intuitive, but it can be inefficient, especially for large numbers, as it checks multiple values until a match is found.

C++ `

#include #include using namespace std;

int lcm(int a, int b) {

// larger value
int g = max(a, b);

// Smaller value
int s = min(a, b);

for (int i = g; i <= a * b; i += g) {
    if (i % s == 0)
        return i;
}

}

int main() { int a = 10, b = 5; cout << lcm(a, b); return 0; }

Java

class GfG {

static int lcm(int a, int b) {
    
    // Larger value
    int g = Math.max(a, b); 
    
    // Smaller value
    int s = Math.min(a, b);

    for (int i = g; i <= a * b; i += g) {
        if (i % s == 0)
            return i;
    }
    return a * b; 
}

public static void main(String[] args) {
    int a = 10, b = 5;
    System.out.println(lcm(a, b)); 
}

}

Python

def lcm(a, b):

# Larger value
g = max(a, b) 

# Smaller value
s = min(a, b)  

for i in range(g, a * b + 1, g):
    if i % s == 0:
        return i
return a * b 

if name == 'main': a = 10 b = 5 print(lcm(a, b))

C#

using System;

class GfG { static int LCM(int a, int b) { // Larger value int g = Math.Max(a, b);

    // Smaller value
    int s = Math.Min(a, b); 

    for (int i = g; i <= a * b; i += g)
    {
        if (i % s == 0)
            return i;
    }
    return a * b;
}

static void Main()
{
    int a = 10, b = 5;
    Console.WriteLine(LCM(a, b)); 
}

}

JavaScript

function lcm(a, b) {

// Larger value
let g = Math.max(a, b);

// Smaller value
let s = Math.min(a, b); 

for (let i = g; i <= a * b; i += g) {
    if (i % s === 0)
        return i;
}
return a * b; 

}

// Driver code let a = 10, b = 5; console.log(lcm(a, b));

`

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

[Expected Approach] Using GCD LCM Formula

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

relation---------between---------hcf---------and---------lcm_________

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.

C++ `

#include using namespace std;

// function for gcd int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }

int lcm(int a, int b) { return (a / gcd(a, b)) * b; }

int main() { int a = 10, b = 5; cout << lcm(a, b); return 0; }

Java

class GfG {

// function for gcd 
static int gcd(int a, int b) {
    return (b == 0) ? a : gcd(b, a % b);
}

static int lcm(int a, int b) {
    return (a / gcd(a, b)) * b;
}

public static void main(String[] args) {
    int a = 10, b = 5;
    System.out.println(lcm(a, b)); 
}

}

Python

function for gcd

def gcd(a, b): return a if b == 0 else gcd(b, a % b)

def lcm(a, b): return (a // gcd(a, b)) * b

if name == 'main': a = 10 b = 5 print(lcm(a, b))

C#

using System;

class GfG { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

static int lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}

static void Main()
{
    int a = 10, b = 5;
    Console.WriteLine(lcm(a, b)); 
}

}

JavaScript

// function for gcd function gcd(a, b) { return b === 0 ? a : gcd(b, a % b); }

function lcm(a, b) { return (a / gcd(a, b)) * b; }

// Driver code let a = 10, b = 5; console.log(lcm(a, b));

`

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