Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
clock() function in C
Next article icon

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, n
Return 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) = 20

Important points:

  1. 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.
  2. 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.
  3. 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.

Next Article
clock() function in C

M

Mrigendra Singh
Improve
Article Tags :
  • Misc
  • Mathematical
  • C++
  • DSA
  • LCM
Practice Tags :
  • CPP
  • Mathematical
  • Misc

Similar Reads

  • list assign() function in C++ STL
    The list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another. To assign elements to a list. Syntax: list_name.assign(count, value) Parameters: This function accepts two mandatory parameters as shown in th
    2 min read
  • list cbegin() and cend() function in C++ STL
    The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the obje
    2 min read
  • Ceil and Floor functions in C++
    In mathematics and computer science, the floor() and ceil() functions that are defined in <cmath> header file, map a real number to the greatest preceding or the least succeeding integer, respectively. C++ floor() FunctionThe floor() function returns the largest integer that is smaller than or
    3 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
  • clock() function in C
    The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
    2 min read
  • isfinite() function in C++
    The isfinite() function is a builtin function in C++ and is used to determine whether a given value if finite or not. A finite value is a value that is neither infinite nor NAN. If the number is finite then the function returns 1 else returns zero.Syntax: bool isfinite(float x); or, bool isfinite(do
    2 min read
  • Measure execution time of a function in C++
    We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we
    3 min read
  • Modulus function in C++ STL
    Modulus function is used to return the value of the modulus between its two arguments. It works same as modulus operator works. template struct modulus : binary_function { T operator() (const T& x, const T& y) const { return x%y; } }; Member types: Type of first argument Type of second argum
    2 min read
  • ldexp() function in C/C++
    The ldexp() is a built-in function in C/C++ gives the product of any variable x and 2 raised to the power of exp by taking two arguments x and exp i.e., x * 2^exp Syntax: float ldexp (float x, int exp) double ldexp (double x, int exp) long double ldexp (long double x, int exp) double ldexp (T x, int
    2 min read
  • norm() function in C++ with Examples
    The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences