C++ Program To Find LCM of Two Numbers
Last Updated :
12 Sep, 2023
LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers.

We can find the LCM of two numbers in C++ using two methods:
1. LCM of Two Numbers Using Simple Method
Algorithm
- Initialize two integers a and b with the two numbers for which we want to find the LCM.
- Initialize a variable max with the maximum of a and b.
- Run an infinite loop. Inside the loop, check if max is completely divisible by a and b, it means that max is the LCM of a and b.
- Else, increment max by 1 and continue the loop to check the next number.
C++ Program To Find LCM of Two Numbers using Simple Method
C++
// C++ program to find the LCM of two
// numbers using the if statement and
// while loop
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 15, b = 20, max_num, flag = 1;
// Use ternary operator to get the
// large number
max_num = (a > b) ? a : b;
while (flag) {
// if statement checks max_num is completely
// divisible by n1 and n2.
if (max_num % a == 0 && max_num % b == 0) {
cout << "LCM of " << a << " and " << b << " is "
<< max_num;
break;
}
// update by 1 on each iteration
++max_num;
}
return 0;
}
OutputLCM of 15 and 20 is 60
Complexity Analysis
- Time complexity: O(a*b)
- Auxiliary space: O(1)
2. LCM of Two Numbers Using Built-In std::lcm() Function
C++ has an inbuilt function lcm() to find the lcm of the two numbers. It is defined in <numeric> header file.
Syntax
lcm(num1, num2);
where num1 and num2 are the two numbers.
Note: This function is only available since C++17.
C++ Program to Find LCM Using std::lcm() Function
C++
// CPP program to illustrate how to find lcm of two numbers
// using std::lcm function
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
cout << "LCM(10,20) = " << lcm(10, 20) << endl;
return 0;
}
Output
LCM(10,20) = 20
Complexity Analysis
- Time complexity: O(log(min(a, b)))
- Auxiliary space: O(1)
3. LCM of Two Numbers Using GCD
An efficient solution is based on the below formula for LCM of two numbers ‘a’ and ‘b’.
a x b = LCM(a, b) * GCD (a, b)
LCM(a, b) = (a x b) / GCD(a, b)
We have discussed the function to find the GCD of two numbers. Using GCD, we can find LCM.
C++ Program to Find LCM Using GCD
C++
// C++ program to find LCM
// of two numbers
#include <iostream>
using namespace std;
// Recursive function to return
// gcd of a and b
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to return LCM of
// two numbers
long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }
// Driver code
int main()
{
int a = 15, b = 20;
cout << "LCM of " << a << " and " << b << " is "
<< lcm(a, b);
return 0;
}
Complexity Analysis
- Time Complexity: O(log(min(a,b))
- Auxiliary Space: O(log(min(a,b))
Refer to the complete article Program to find LCM of two numbers for more details.
Related Articles
Similar Reads
Program to find LCM of two Fibonacci Numbers Given here are two positive numbers a and b. The task is to print the least common multiple of a'th and b'th Fibonacci Numbers.The first few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ......Note that 0 is considered as 0âth Fibonacci Number.Examples: Input : a = 3, b = 12 Ou
8 min read
C++ Program for Common Divisors of Two Numbers Given two integer numbers, the task is to find the count of all common divisors of given numbers. Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors a
2 min read
GCD of Two Numbers in C++ GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that exactly divides both numbers. In this article, we will learn to write a C++ program to find the GCD of two numbers.Example:Input: a = 12, b = 16Output: 4Explanation: As 4 is the largest number whic
3 min read
Find the ratio of LCM to GCD of a given Array Given an array arr[] of positive integers, the task is to find the ratio of LCM and GCD of the given array.Examples: Input: arr[] = {2, 3, 5, 9} Output: 90:1 Explanation: The GCD of the given array is 1 and the LCM is 90. Therefore, the ratio is evaluated as 90:1.Input: arr[] = {6, 12, 36} Output: 6
7 min read
Find two numbers with the given LCM and minimum possible difference Given an integer X, the task is to find two integers A and B such that LCM(A, B) = X and the difference between the A and B is minimum possible.Examples: Input: X = 6 Output: 2 3 LCM(2, 3) = 6 and (3 - 2) = 1 which is the minimum possible.Input X = 7 Output: 1 7 Approach: An approach to solve this p
5 min read