###### C++ Decision Making

C++ Program to Find LCM.

# C++ Program to Find LCM

In this example, we will learn Examples on different ways to calculate the LCM (Lowest Common Multiple) of two integers using loops and decision making statements.

To understand this example, you should have the knowledge of the following C++ programming topics:

## LCM (Lowest Common Multiple)

LCM of two integers a and b is the smallest positive integer that is divisible by both a and b.

For example: LCM of 8 and 12 is 24, and LCM of 5 and 8 is 40

## Example 1: Program to Find LCM using do-while loop

``````#include <iostream>
using namespace std;

int main()
{
int n1, n2, max;

cout << "Enter two numbers: ";
cin >> n1 >> n2;

// maximum value between n1 and n2 is stored in max
max = (n1 > n2) ? n1 : n2;

do
{
if (max % n1 == 0 && max % n2 == 0)
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true);

return 0;
}``````

Output

```Enter two numbers: 8
14
LCM = 56```
##### Working

In above program, user is asked to integer two integers n1 and n2 and largest of those two numbers is stored in max.

It is checked whether max is divisible by n1 and n2, if it's divisible by both numbers, max (which contains LCM) is printed and loop is terminated.

If not, value of max is incremented by 1 and same process goes on until max is divisible by both n1 and n2.

## Example 2: Program to LCM using HCF

The LCM of two numbers is given by:

`LCM = (n1 * n2) / HCF`

``````#include <iostream>
using namespace std;

int main()
{
int n1, n2, hcf, temp, lcm;

cout << "Enter two numbers: ";
cin >> n1 >> n2;

hcf = n1;
temp = n2;

while(hcf != temp)
{
if(hcf > temp)
hcf -= temp;
else
temp -= hcf;
}

lcm = (n1 * n2) / hcf;

cout << "LCM = " << lcm;
return 0;
}``````

Output

```Enter two numbers: 14
16
LCM = 424```

## Next Example

We hope that this Example helped you develop better understanding of the concept of "Find LCM" in C++.

Keep Learning : )

In the next Example, we will learn about C++ `Program to Find Reverse a Number`.