Find LCM (Least Common Multiple) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

  

C Program

#include<stdio.h>

int gcd(int a, int b) {
    while (b) { int t = b; b = a % b; a = t; }
    return a;
}

int main() {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d", a * b / gcd(a, b));
}

C Output

Input: 4 6  
Output: 12


C++ Program

#include<iostream>
using namespace std;

int gcd(int a, int b) {
    while (b) { int t = b; b = a % b; a = t; }
    return a;
}

int main() {
    int a, b;
    cin >> a >> b;
    cout << a * b / gcd(a, b);
}

C++ Output

Input: 5 7  
Output: 35


JAVA Program

import java.util.*;

class L {
    static int gcd(int a, int b) {
        while (b != 0) { int t = b; b = a % b; a = t; }
        return a;
    }

    public static void main(String[] a) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt(), y = sc.nextInt();
        System.out.print(x * y / gcd(x, y));
    }
}

JAVA Output

Input: 10 15  
Output: 30


Python Program

a, b = map(int, input().split())
def gcd(x, y):
    while y: x, y = y, x % y
    return x
print(a * b // gcd(a, b))

Python Output

Input: 8 12  
Output: 24


In-Depth Learning – Entire Concept in Paragraphs
What Is LCM?
LCM (Least Common Multiple) of two integers is the smallest number that is both divisible by them. That is, it's the smallest positive integer which is divisible by both a and b. For example, LCM of 4 and 6 is 12 since 12 is the first number which features in both their multiplication table.

LCM is extensively utilized in time synchronization, adding fractions, calculating gear ratio, and calendar/date synchronizations.

How the Code Works
The most efficient method to compute LCM is through the formula:

LCM(a, b) = (a × b) / GCD(a, b)

So first we get the GCD of the two numbers by using the Euclidean Algorithm, which gets the greatest common divisor by repeatedly doing a % b until b is 0. After knowing the GCD, we divide the product of the two numbers by the GCD to obtain the LCM.

This method is both mathematically sound and computationally efficient. It ensures minimal overflow risk compared to finding LCM by listing multiples or using loops.

Example
Let’s take two numbers: 10 and 15.

Step 1: GCD(10, 15)
→ 15 % 10 = 5, 10 % 5 = 0 → GCD = 5

Step 2: LCM = (10 × 15) / 5 = 150 / 5 = 30

Another example: 4 and 6

GCD = 2

LCM = (4 × 6) / 2 = 12

Real-Life Analogy
Suppose two traffic lights — one blinks every 4 seconds and the other blinks every 6 seconds. LCM is useful in finding out when both will blink simultaneously. Here it is at 12 seconds — which is their LCM. This comes in handy in real-life scheduling problems, including train schedules, event scheduling, and circular gear motion synchronization.

Why LCM is Important in Programming
LCM is taught the modular arithmetic, GCD/LCM relationships, and mathematical optimization to make efficient programs. It is used quite often in interview problems and competitive programming challenges that include working with time intervals, multiples, or resolving Diophantine equations.

The concept of LCM together with GCD also facilitates the resolution of more complex topics such as:

LCM of arrays

LCM with constraints

Chinese Remainder Theorem

Cryptographic key generation logic

Python-Specific Advantage
Python's method keeps it readable and brief. In only a couple of lines, you can implement GCD with a while loop and then just insert it straight into the LCM formula with // (floor division). It's an excellent lesson in tidy logic and function reuse.

SEO-Optimized Natural Paragraph for Ranking
If you are searching for the most effective method to calculate the LCM of two numbers, this tutorial presents straightforward and correct solutions in C, C++, Java, and Python. Based on the mathematical relationship between GCD and LCM, this approach is both efficient and simple to code. Suitable for students, programmers, or interview aspirants, this technique imparts the construction of logic and mathematical operations to a programmer's perspective. Whether you're dealing with calendar calendars, coordinating events, or mastering algorithmic puzzles, knowing how to calculate LCM will provide you with a critical skill in fundamental and intermediate programming.