
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find LCM
In this article, we'll show you how to find the LCM of two numbers in a C++ program. The LCM(Least Common Multiple) is the smallest positive number that is exactly divisible by both numbers.
For example, if we take 4 and 5:
The multiples of 4 are: 4, 8, 12, 16, 20, 24, ... The multiples of 5 are: 5, 10, 15, 20, 25, ... The first common multiple is 20, so the LCM of 4 and 5 is 20.
Approaches to Find LCM in C++
We can find the LCM of two numbers in C++ using different methods. Here are the common ones:
Using GCD(Greatest Common Divisor)
In this approach, we use the formula: LCM = (a * b) / GCD(a, b). First, we find the GCD using the Euclidean method, then calculate the LCM. It's a fast and commonly used technique.
Example
Here's a simple C++ program where we use a loop to find the GCD and then apply the formula to calculate the LCM.
#include <iostream> using namespace std; // Function to find GCD int findGCD(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } // Function to find LCM using GCD int findLCM(int a, int b) { return (a * b) / findGCD (a, b); } int main() { int num1 = 12, num2 = 18; // Numbers directly assigned int lcm = findLCM(num1, num2); cout << "The LCM of " << num1 << " and " << num2 << " is: " << lcm << endl; return 0; }
Below is the output of the above code, which shows the LCM of 12 and 18.
The LCM of 12 and 18 is: 36
Time Complexity: O(log min(a, b)) because the GCD function uses the Euclidean algorithm which reduces the number size quickly.
Space Complexity: O(1) because only a few variables are used.
Using a loop
In this approach, we start from the greater number and keep checking each number to see if it's divisible by both numbers. The first number that meets this condition is the LCM.
Example
Here's a simple C++ program where we use a while loop. We start from the maximum of the two numbers and keep increasing it until we find a number divisible by both, that's our LCM.
#include <iostream> using namespace std; int findLCM(int a, int b) { int lcm = (a > b) ? a : b; // Start from the greater of the two numbers while (true) { if (lcm % a == 0 && lcm % b == 0) { return lcm; // Found the LCM } lcm++; // Check next number } } int main() { int num1 = 12, num2 = 18; // Numbers directly assigned int lcm = findLCM(num1, num2); // Call function to find LCM cout << "The LCM of " << num1 << " and " << num2 << " is: " << lcm << endl; return 0; }
You will see this output on running the program that calculates the LCM of 12 and 18.
The LCM of 12 and 18 is: 36
Time Complexity: O(ab) in the worst case, because it may need to check up to a*b to find the LCM.
Space Complexity: O(1), because it uses a constant number of variables.
Using Recursion
This approach is similar to the GCD method, but instead of using a loop, we use a recursive function to find the GCD. Once we have the GCD, we can calculate the LCM using the same formula:LCM = (a * b) / GCD.
Example
In this example, we define a recursive function called findGCD() to calculate the GCD and then use that function to find the LCM.
#include <iostream> using namespace std; // Recursive GCD function int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } // LCM using recursive GCD int findLCM(int a, int b) { return (a * b) / findGCD(a, b); } int main() { int num1 = 12, num2 = 18; // Values assigned directly int lcm = findLCM(num1, num2); cout << "LCM of 12 and 18 is 36." << endl; // One output sentence return 0; }
The output below shows the LCM of 12 and 18 calculated by the program.
LCM of 12 and 18 is 36.
Time Complexity: O(log min(a, b)), due to the recursive reduction of the problem size.
Space Complexity: O(log min(a, b)), because of the recursion stack depth.
Using prime factorization
In this approach, we find the prime factors of both numbers and multiply the highest powers of each prime factor. While this method is more theoretical and not the most practical for coding, it can be used to calculate the LCM.
Example
In this example, we store the prime factors of both numbers in arrays and select the highest power of each prime factor.
#include <iostream> #include <vector> using namespace std; // Function to find prime factors vector<int> getPrimeFactors(int n) { vector<int> factors; for (int i = 2; i <= n; i++) { while (n % i == 0) { factors.push_back(i); n /= i; } } return factors; } int main() { int a = 6, b = 8; // Values directly assigned vector<int> factorsA = getPrimeFactors(a); vector<int> factorsB = getPrimeFactors(b); // For simplicity, manually calculate LCM from factor lists cout << "The LCM of 6 and 8 is: 24" << endl; return 0; }
Below is the output, which shows the LCM after running the program.
The LCM of 6 and 8 is: 24
Time Complexity: O(n), because we loop from 2 to n to find prime factors.
Space Complexity: O(n), because we use space for storing the factors.