C++ Program To Find LCM of Two Numbers

Last Updated : 31 Mar, 2026

LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers.

LCM in C++

We can find the LCM of two numbers using the following methods:

Simple Method

  • Initialize two integers a and b with the two numbers for which we want to find the LCM.
  • Initialize a variable max with the maximum of a and b.
  • Run an infinite loop. Inside the loop, check if max is completely divisible by a and b, it means that max is the LCM of a and b.
  • Else, increment max by 1 and continue the loop to check the next number.

Code Implementation:

C++
#include <iostream>
using namespace std;

// Driver code
int main()
{
    int a = 15, b = 20, max_num, flag = 1;

    // Use ternary operator to get the
    // large number
    max_num = (a > b) ? a : b;

    while (flag) {
        // if statement checks max_num is completely
        // divisible by n1 and n2.
        if (max_num % a == 0 && max_num % b == 0) {
            cout << "LCM of " << a << " and " << b << " is "
                 << max_num;
            break;
        }

        // update by 1 on each iteration
        ++max_num;
    }
    return 0;
}

Output
LCM of 15 and 20 is 60

Complexity Analysis

  • Time complexity: O(max(a, b)) in the worst case (especially when the numbers are co-prime).
  • Auxiliary space: O(1)

Using Built-In std::lcm() Function

C++ has an inbuilt function lcm() to find the lcm of the two numbers. It is defined in <numeric> header file.

Syntax

lcm(num1, num2);

where num1 and num2 are the two numbers.

Note: This function is only available since C++17.

Code Implementation

C++
#include <iostream>
#include <numeric>

using namespace std;

int main()
{
    cout << "LCM(10,20) = " << lcm(10, 20) << endl;
    return 0;
}

Output
LCM(10,20) = 20

Complexity Analysis

  • Time complexity: O(log(min(a, b)))
  • Auxiliary space: O(1)

Using GCD

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 the function to find the GCD of two numbers. Using GCD, we can find LCM.

Code Implementation:

C++
#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 code
int main()
{
    int a = 15, b = 20;
    cout << "LCM of " << a << " and " << b << " is "
         << lcm(a, b);

    return 0;
}
Try It Yourself
redirect icon

Complexity Analysis

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

Refer to the complete article Program to find LCM of two numbers for more details.

Program to find LCM of 2 numbers without using GCD

Comment