![]() |
VOOZH | about |
The lcm() is a built-in function introduced in C++17. It is used to calculate the least common multiple (LCM) of two integers. The LCM of two integers is the smallest positive integer that is divisible by both numbers.
Let’s take a look at an example that illustrates the lcm() function:
Output
36This article covers the syntax, usage, and common examples of the lcm() function in C++:
The lcm() function is defined in the <numeric> header.
lcm(a, b);
Parameters:
Return Value:
Time Complexity: O(log(min(a,b)))
Auxiliary Space: O(1)
Note: The lcm() function works only on integer data type, and if any other data type like char, double, is provided in its argument, then it will throw an error.
The following code examples demonstrates how we can use the lcm() function in different scenarios:
Output
60We can use the accumulate() function along with lcm() function from <numeric> to compute the LCM of multiple numbers in a vector.
Output
24