LCM of Two Numbers 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.

C++ Program to Find LCM Using std::lcm() Function

C++




// CPP program to illustrate how to find lcm of two numbers
// using std::lcm function
#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)

C++ Program To Find LCM of Two Numbers

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.

We can find the LCM of two numbers in C++ using two methods:

Similar Reads

1. LCM of Two Numbers Using Simple Method

Algorithm...

2. LCM of Two Numbers Using Built-In std::lcm() Function

...

3. LCM of Two Numbers Using GCD

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

Contact Us