In this post, we will learn **how to find the lcm of two numbers using C++** Programming language.

The** LCM (Least Common Multiple) **of two numbers **a** and **b** is the smallest positive integer that is perfectly divisible by both** a** and **b**.

We will be finding the lcm of two numbers using the following ways:

- Using Standard Method
- Using GCD

So, without further ado, let’s begin this tutorial.

**C++ Program to Find LCM of Two Numbers**

// C++ Program to Find LCM of Two Numbers #include <iostream> using namespace std; int main(){ int a, b, lcm; // Asking for input cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; lcm = (a > b) ? a : b; //greatest of the two while(1){ if ((lcm % a == 0) && (lcm % b == 0)){ cout << "LCM of two numbers: " << lcm; break; } ++lcm; } return 0; }

**Output**

```
Enter the first number: 12
Enter the second number: 15
LCM of two numbers: 60
```

**How Does This Program Work ?**

int a, b, lcm;

In this program, we have declared three** int data type** variables named **a**, **b** and **lcm**.

// Asking for input cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b;

Then, the user is asked to enter two integers. The value of these two integers will get stored in **a** and **b** named variables.

while(1){ if ((lcm % a == 0) && (lcm % b == 0)){ cout << "LCM of two numbers: " << lcm; break; } ++lcm; }

Now, we check whether the maximum value is divisible by both **a** and **b** or not. If yes, then the value of **lcm** variables is printed and our program terminates.

If not, then* lcm* is incremented by **1** and the same process goes on until lcm is exactly divisible by both **a** and **b**.

**C++ Program to Find LCM of Two Numbers Using HCF**

// C++ Program to Find the LCM of Two Numbers Using HCF #include <iostream> using namespace std; int main(){ int a, b, i, gcd, lcm; // Asking for input cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; // Calculating GCD for (i = 1; i <= a && i <= b; ++i){ if (a % i == 0 && b % i == 0) gcd = i; } // Calculating lcm using hcf lcm = (a * b) / gcd; cout << "LCM of two numbers: " << lcm << endl; return 0; }

**Output**

```
Enter the first number: 3
Enter the second number: 5
LCM of two numbers: 15
```

**Conclusion**

I hope after going through this post, you understand** how to find the lcm of two numbers using C++ **Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

**Also Read:**