DEV Community

Cover image for Factor Operations: gcd and lcm functions in C++
Satyam Lachhwani
Satyam Lachhwani

Posted on

1 1

Factor Operations: gcd and lcm functions in C++

Oftentimes, we need to find GCD (Greatest Common Divisor) and LCM (Lowest Common Multiple) of two numbers and we tend to implement our own functions in C++ for this purpose.

If you don't know yet, you'll be glad to hear that there are native functions provided by C++ to find GCD and LCM of two integers. It is as simple as that:

int a = 150, b = 225;
int gcd_num = gcd(a, b); // returns 75
int lcm_num = lcm(a, b); // returns 450
Enter fullscreen mode Exit fullscreen mode

Let's now move forward and know more about this function.

Use

Include the required header file in order to be able to use the gcd or lcm function. You can either include bits/stdc++.h or include numeric on the top of your .cpp file.

Accepted function parameters

These two functions accept two positive integers as parameters and may throw an error that differs from compiler to compiler or give unexpected results in case unexpected parameters are passed.

Things to keep in mind

  • If both numbers are zero, gcd() returns zero.
  • If any of the numbers is zero, lcm() returns zero.

There is an interesting relation between the numbers and their respective gcd and lcm. If two numbers are represented as m and n, then

m * n = gcd * lcm
Enter fullscreen mode Exit fullscreen mode

If you already knew this, it is great. If you didn't then now you know. If you know any other interesting things related to it, please write them in the comments.

For full reference, visit gcd and lcm

If you find anything wrong or if you know some more exciting functions or other stuff related to C++, please drop a comment below so we learn together.

My Profile -

Github
LinkedIn

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
pgradot profile image
Pierre Gradot

Once I discover new functions in the standard library XD

Don't use bits/stdc++.h in real life code: geeksforgeeks.org/bitsstdc-h-c/

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay