C++ Program to Find the LCM of two numbers (3 Ways)

In this program, we will learn how to find the LCM of two numbers using the C++ programming language.

What is LCM?

it stands for Least Common Multiple. It can be found for two or more numbers by making multiple of the numbers and then choosing the least common multiple.

Let’s take the example of 3 and 4.
Multiple of 3 : 3, 6, 9, 12, 15, 18, 21, 24,…….
Multiple of 4 : 4, 8, 12, 16, 20, 24, 28,…..
So, common multiple of 3 and 4 is 12, 24 and Least Common Multiple is 12.
Hence, LCM of 3 and 4 is 12.

We will discuss three ways to write code for it.

  • Using the loop
  • Using the GCD or HCF
  • Using the Recursion

C++ Programming Code to Find the LCM of two numbers

Using the loop

In this program, I have used the while loop to find the least common multiple of both the numbers.

Code:-

#include <iostream>

using namespace std;

int main() {
    int num1, num2, lcm, mul;

    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;

    lcm = (num1 > num2) ? num1 : num2;

    while(true) {
        if( lcm%num1 == 0 && lcm%num2 == 0 ) {
            cout << "The LCM of " << num1 << " and " << num2 << " is " << lcm << endl;
            break;
        }
        lcm += lcm;
    }

    return 0;
}

Output:-

Enter the first number: 27
Enter the second number: 6
The LCM of 27 and 6 is 54

Using the GCD or HCF

In this program, I have used a mathematical formula.

Formula:
Product of LCM and HCF is equal to product of the numbers.

I have already discussed how to find the GCD of the numbers. You can learn all the ways to do it.

Code:-

#include <iostream>

using namespace std;

int getGCD(int num1, int num2){
    int gcd;
    for(int i=1; i <= num1 && i <= num2; ++i){
        if(num1 % i == 0 && num2 % i == 0)
        gcd = i;
    }
    return gcd;
}

int main(){
    int num1, num2, gcd, lcm;

    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;

    gcd = getGCD(num1, num2);

    lcm = (num1 * num2) / gcd;
    cout << "The LCM of " << num1 << " and " << num2 << " is " << lcm << endl;

    return 0;
}

Output:-

Enter the first number: 81
Enter the second number: 33
The LCM of 81 and 33 is 891

Using the Recursion

In this program, I have used the findLCM() recursive function.

Code:-

#include <iostream>

using namespace std;
 
int findLCM(int x, int y);
 
 
int main(){
    int num1, num2, lcm;
 
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
 
    if(num1 > num2)
        lcm = findLCM(num2, num1);
    else
        lcm = findLCM(num1, num2);
 
    cout << "The LCM of " << num1 << " and " << num2 << " is " << lcm << endl;
 
    return 0;
}
 
int findLCM(int x, int y){
    static int multiple = 0;

    multiple += y;

    if((multiple % x == 0) && (multiple % y == 0)){
        return multiple;
    }
    else{
        return findLCM(x, y);
    }
}

Output:-

Enter the first number: 6
Enter the second number: 9
The LCM of 6 and 9 is 18

You can learn about many other C++ Programs Here.

Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...