C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

How To Compute The Greatest Common Divisor And Least Common Multiple in C++

The C++ 17 standard brings us a lot of useful methods, templates, numerics, and algorithms. Two of the great numeric functions are std::gcm and std::lcm which are defined in the <numeric> header that computes the greatest common divisor and least common multiple of two integers. In this post, we learn what std::gcm and std::lcm are, and how can we use them in modern C++.

What is std::gcm the greatest common divisor numeric function in C++?

In mathematics, the Greatest Common Divisor (GCD) also known as the Highest Common Factor (HCF) of two integer numbers is the largest number that exactly divides both numbers. In C++, the std::gcm greatest common divisor numeric function is defined in the <numeric> header that computes the greatest common divisor of the given two integers. 

Here is the syntax of std::gcm since C++17,

Here how we can find the greatest common divisor of two numbers in C++,

this will return 23 because both has 23 divisor which is the greatest one.

What is the std::lcm least common multiple numeric function in C++?

In mathematics, the Least Common Multiple (LCM) also known as the Lowest Common Multiple, or Smallest Common Multiple of two integer numbers is the lowest common multiple number of two integers In C++, the std::lcm least common multiple numeric function is defined in the <numeric> header that computes the lowest multiple number of the given two integers.

Here is the syntax of std::lcm since C++17,

Here how we can find the lowest common multiple, of two numbers in C++,

this will return 12 because 12 is the lowest common multiple of 12 and 6.

Is there a full example about std::gcm and std::lcm numeric functions in C++?

Here is a full example about std::gcm and std::lcm numeric functions in modern C++,

For more details about this feature in C++17 standard, please see these papers; P0295R0

How To Compute The Greatest Common Divisor And Least Common Multiple in C++

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Wide Strings In C++?

C++C++17C++20C++23Learn C++Syntax

How To Use std::make_from_tuple in C++ 17