In this example, you will about C++ program to find LCM (Lowest Common Multiple) using two different methods. You will also learn to find LCM using GCD.
What is LCM?
LCM (Lowest Common Multiple) of two numbers is the smallest positive integer that is perfectly divisible by the given numbers.
//C++ program to find LCM
#include <iostream>
using namespace std;
int main()
{
int num1, num2, maxValue;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
maxValue = (num1 > num2) ? num1 : num2;
while(1)
{
//condition of LCM
if((maxValue % num1 == 0) && (maxValue % num2 == 0))
{
cout << "LCM: " << maxValue << endl;
break;
}
++maxValue;
}
return 0;
}
Output

Explanation
Maximum value between two numbers is computed using conditional operator which is store in maxValue.
Every time maxValue is checked whether it is perfectly divisible by both numbers or not.
If maxVaule is perfectly divisible, it is the LCM otherwise maxValue is increased by 1.
Formula for computing LCM using GCD:
LCM = (num1 * num2) / GCD
Let’s implement the formula in C++ program.
//C++ program to find LCM using GCD
#include <iostream>
using namespace std;
int main()
{
int num1, num2, i, gcd, lcm;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
//calculation of gcd
for(i=1; i <= num1 && i <= num2; ++i)
{
if(num1 % i == 0 && num2 % i == 0)
gcd = i;
}
//calculation of lcm using gcd
lcm = (num1 * num2) / gcd;
cout << "LCM: " << lcm << endl;
return 0;
} //end main
Output
Enter two numbers: 20 30 LCM: 60