C++ Program to Find LCM of Two Numbers

C++ Program to Find LCM of Two Numbers

The Least Common Multiple (LCM) of two numbers is the smallest number that is evenly divisible by both numbers. For example, the LCM of 4 and 6 is 12. LCM is widely used in mathematics and programming, particularly in problems involving fractions, synchronization of cycles, or scheduling tasks. Understanding how to calculate the LCM in C++ not only helps beginners practice loops and functions but also builds a foundation for more complex number theory problems. In this article, we will explore multiple beginner-friendly C++ programs to find the LCM of two numbers.

Program 1: Using a Simple Loop

The most straightforward method to find the LCM is by checking multiples of the larger number until we find one divisible by both numbers.

#include <iostream>
using namespace std;

int main() {

    int a, b, max;

    cout << "Enter two numbers: " << endl;
    cin >> a >> b;

    max = (a > b) ? a : b; // Start from the larger number

    while(true) {

        if(max % a == 0 && max % b == 0) {

            cout << "LCM of " << a << " and " << b << " is " << max << endl;

            break;

        }

        max++;

    }

    return 0;

}

In this program, we start from the larger of the two numbers because the LCM cannot be smaller than the maximum. The loop checks each number to see if it is divisible by both a and b. Once a number meets this condition, it is the LCM. This method is very simple and helps beginners understand the concept of multiples, although it can be inefficient for large numbers.

Program 2: Using GCD to Calculate LCM

A more efficient method uses the relationship between GCD and LCM:

$$
\text{LCM}(a, b) = \frac{a \times b}{\text{GCD}(a, b)}
$$

#include <iostream>
#include <numeric> // Required for std::gcd

using namespace std;

int main() {

    int a, b;

    cout << "Enter two numbers: " << endl;
    cin >> a >> b;

    int gcdValue = gcd(a, b); // Calculate GCD using built-in function
    int lcmValue = (a * b) / gcdValue; // LCM formula

    cout << "LCM of " << a << " and " << b << " is " << lcmValue << endl;

    return 0;

}

Here, we first calculate the GCD using std::gcd() from the <numeric> library. Then, we use the formula LCM = (a * b) / GCD to compute the LCM efficiently. This method is faster than checking multiples and is widely used in real-world programming. Beginners can see how combining known mathematical relationships with programming functions leads to efficient solutions.

Program 3: Using the Built-in std::lcm() Function (C++17 and Later)

Modern C++ makes calculating the LCM even easier with the built-in std::lcm() function from the <numeric> library. This method is simple, efficient, and recommended for real-world programs.

#include <iostream>
#include <numeric> // Required for std::lcm

using namespace std;

int main() {

    int a, b;

    cout << "Enter two numbers: " << endl;
    cin >> a >> b;

    int result = lcm(a, b); // std::lcm computes the least common multiple

    cout << "LCM of " << a << " and " << b << " is " << result << endl;

    return 0;

}

In this program, the lcm() function directly returns the least common multiple of a and b. You don’t need to write loops or implement formulas manually. Using std::lcm() is concise, easy to read, and works efficiently with very large numbers. While learning loops or GCD-based formulas is important for understanding the logic, in practical coding, std::lcm() saves time and reduces the chance of errors.

Program 4: LCM of Multiple Numbers

You are not limited to just two numbers. By applying the LCM formula iteratively with std::lcm(), you can find the LCM of three or more numbers.

#include <iostream>
#include <numeric> // Required for std::lcm

using namespace std;

// Function to calculate LCM of multiple numbers in an array
int findLCMofArray(int arr[], int n) {

    int result = arr[0]; // Start with the first number

    for(int i = 1; i < n; i++) {
        result = lcm(result, arr[i]); // Iteratively apply std::lcm
    }

    return result;

}

int main() {

    int numbers[] {12, 15, 20};
    int n = sizeof(numbers) / sizeof(numbers[0]);

    int lcmResult = findLCMofArray(numbers, n);

    cout << "LCM of the numbers is " << lcmResult << endl;

    return 0;

}

In this program, findLCMofArray() iteratively applies std::lcm() to all numbers in the array. By the end of the loop, result contains the LCM of all numbers. This method is efficient and demonstrates how modern C++ functions can handle multiple values cleanly and reliably.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about LCM in C++:

Q1: Which method is best for real programs?
Using the LCM function (like std::lcm() or a GCD-based LCM function) is recommended because it is fast, efficient, and works reliably even for large numbers.

Q2: Can LCM be smaller than the numbers?
No, the LCM is always equal to or larger than the largest of the given numbers.

Q3: Can I calculate LCM of more than two numbers?
Yes, by applying the LCM function iteratively on multiple numbers, you can find the LCM of three or more numbers.

Q4: Why learn the simple loop method?
The loop method helps beginners understand multiples and the basic concept of LCM before moving on to optimized approaches using an LCM function.

Conclusion

Calculating the LCM of two numbers in C++ is an essential skill for beginners, combining math and programming logic. We explored multiple methods: a simple loop, using GCD to calculate LCM, and the built-in LCM function for concise, modular code. While the loop method is excellent for learning the concept of multiples, the GCD-based approach or the LCM function is efficient and suitable for real applications. By practicing these methods, beginners can strengthen their understanding of number operations, functions, and algorithmic thinking, preparing them for more complex programming challenges.

Additional & References

LCM calculations are foundational for problems involving fractions, cycles, and scheduling tasks. Beginners should try calculating LCM for multiple numbers and explore how it relates to GCD for a deeper understanding of number theory in programming.

Scroll to Top