Inbuilt function for calculating LCM in C++ Last Updated : 14 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Many times while we do programming, we need to calculate the Least Common Multiple (LCM) between two numbers. We have already discussed how to find LCM in this post. In place of defining and then using a function for calculating lcm , we can simply use an inbuilt function of boost library of C++ , boost::math::lcm (). For using this function , we have to declare a header file <boost/math/common_factor.hpp>. Syntax: boost::math::lcm (m,n)Parameters: m, nReturn Value: 0 if either m or n are zero,else lcm of mod(m) and mod(n). CPP // CPP program to illustrate // boost::math::lcm function of C++ #include <iostream> #include <boost/math/common_factor.hpp> using namespace std; int main() { cout << "LCM(10,20) = " << boost::math::lcm(10,20) << endl; return 0; } Output: LCM(10,20) = 20Important points:The function will calculate the lcm after taking the modulus of both the numbers, so in case if any of the number being negative, it will be converted to its modulus and then LCM is calculated.In case if any of the number being a non-integer data type , then this function will throw an error. CPP // CPP program to illustrate illegal // behaviour of boost::math::lcm function of C++ #include <iostream> #include <boost/math/common_factor.hpp> using namespace std; int main() { cout << "LCM(1.0,20) = " << boost::math::lcm(1.0,20) << endl; return 0; } This code will throw an error, as one of the argument of the function is a double type, so this code will not work. In C++17, a new STL function for calculating LCM of two numbers, std::lcm(), has been introduced, which can be used on any compiler supporting C++17 features. Comment More infoAdvertise with us Next Article Inbuilt function for calculating LCM in C++ M Mrigendra Singh Improve Article Tags : Misc Mathematical C++ DSA LCM +1 More Practice Tags : CPPMathematicalMisc Similar Reads Ceil and Floor functions in C++ C++ provides floor() and ceil() functions, defined in the <cmath> header file, to find the rounded-down and rounded-up values of floating-point numbersfloor() Functionfloor() function takes floating point number as input and returns the largest integer that is smaller than or equal to the valu 2 min read Find Landau's function for a given number N Given an integer N, the task is to find the Landau's Function of the number N. In number theory, The Landau's function finds the largest LCM among all partitions of the given number N. For Example: If N = 4, then possible partitions are:1. {1, 1, 1, 1}, LCM = 12. {1, 1, 2}, LCM = 23. {2, 2}, LCM = 2 15+ min read lldiv() function in C++ STL The lldiv() is a builtin function in C++ STL which gives us the quotient and remainder of the division of two numbers. Syntax: lldiv(n, d) Parameters: The function accepts two mandatory parameters which are described below: n: It specifies the dividend. The data-type can be long long or long long in 2 min read Top 10 Most Used Inbuilt C++ functions for Competitive Programming In this article, we will discuss about the 10 most used inbuilt functions of C++ which will help you to save time and make code concise as well during competitive programming. List of top 10 inbuilt functions in C++ pow()sqrt()min()max()swap()gcd()toupper()tolower()floor()ceil()1. pow( ) This functi 7 min read fma() function in C++ The fma() function takes three arguments a, b and c, and returns a*b+c without losing precision. The fma() function is defined in the cmath header file. If any argument passed to fma() is long double, the return type is long double. If not, the return type is double. Syntax: double fma(double a, dou 2 min read Like