Definition
Smallest m
Positive m with a | m and b | m — the first shared multiple.
The least common multiple is the smallest shared positive multiple of two integers. This tutorial covers the gcd–lcm identity, a safe C formula with long long, a brute multiple scan for intuition, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
Smallest m
Positive m with a | m and b | m — the first shared multiple.
gcd · lcm
gcd(a,b) · lcm(a,b) = a · b for nonnegative inputs.
(a/g)*b
Divide by gcd before multiplying; use long long for the product.
Multiples
Step by max(a,b) until both divide — pedagogical, slower.
a & b
See gcd and lcm for nonnegative safe integers instantly.
lcm(0,·)=0
Convention on this page: return 0 if either argument is zero.
The least common multiple of positive integers a and b is the smallest positive integer m such that both a and b divide m. For 12 and 18, that value is 36.
In C interviews you typically implement Euclid gcd, then apply lcm = (a / gcd) * b in a wide type — mentioning overflow and the lcm(0, ·) convention.
LCM shows up in scheduling, fraction arithmetic, and any problem that needs a shared period or denominator — and it is the natural follow-up once you know gcd.
One identity gives a fast formula.
(a / g) * b beats (a * b) / g in int.
Widen the product when results may exceed INT_MAX.
Return 0 when either input is zero.
In short: compute g = gcd(a,b), then return (a / g) * b in a wide type — or 0 if either argument is zero.
Given nonnegative integers a and b, compute lcm(a,b). Prefer the gcd formula; optionally show a brute multiple scan for the same inputs.
/* a = 12, b = 18
* gcd = 6
* lcm = (12 / 6) * 18 = 2 * 18 = 36
*
* Multiples of 12: 12, 24, 36, ...
* Multiples of 18: 18, 36, ...
* First common positive multiple: 36
*/ | Item | Type | Description |
|---|---|---|
a, b | int | Nonnegative integers (examples use 12 and 18). |
| Result | long long / int | Least common multiple (or 0 if either input is zero). |
function gcd(a, b): // nonnegative
while b != 0:
(a, b) = (b, a mod b)
return a
function lcm(a, b):
if a = 0 or b = 0:
return 0
g = gcd(a, b)
return (a / g) * b | Method | Idea | Extra space |
|---|---|---|
| gcd + formula | (a / g) * b after Euclid | O(1) |
| Brute scan | Step by max(a,b) until both divide | O(1) |
| Goal | Pattern |
|---|---|
| Euclid step | num2 = num1 % num2; num1 = temp; |
| Safe lcm | return (long long)a / g * (long long)b; |
| Zero inputs | if (a == 0 || b == 0) return 0LL; |
| Brute step | step = a > b ? a : b; m += step; |
| Print wide | printf("%lld\\n", lcm); |
Three ways to think about lcm — interviews almost always want the gcd formula.
(a/g)*bFast, standard interview answer
step maxShows the definition; can be slow
max expUseful when factor tables already exist
overflowMention divide-first and long long
Reach for LCM when you need a shared multiple or period.
After Euclid, ask for lcm via the identity.
Two events repeating every a and b units meet at lcm.
Common denominators use lcm of the bottoms.
lcm(a,b,c) = lcm(lcm(a,b), c).
Prefer gcd when values can be large.
Key benefit: you get a correct lcm from a tiny Euclid loop plus one careful multiply — with a clear story about overflow.
Enter two nonnegative integers and see gcd and lcm (same identity as the C examples).
Two complete C programs — lcm from gcd with long long, and a brute multiple scan. Both use 12 and 18 → 36. Click View Output to reveal sample console results.
Preferred interview approach: Euclid then divide-before-multiply.
12, 18)Uses long long and divides by gcd before the final multiply so the product is safer in practice.
#include <stdio.h>
int find_gcd(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
long long find_lcm_ll(int num1, int num2) {
int g;
if (num1 == 0 || num2 == 0) {
return 0LL;
}
g = find_gcd(num1, num2);
return (long long)num1 / g * (long long)num2;
}
int main(void) {
int number1 = 12;
int number2 = 18;
long long lcm = find_lcm_ll(number1, number2);
printf("LCM of %d and %d is: %lld\n", number1, number2, lcm);
return 0;
} With g = 6, 12/6 = 2 and 2 · 18 = 36. This matches (12 · 18) / 6 = 216 / 6 without forming 12 * 18 in a narrow int first.
Walk the definition along multiples of the larger input.
No explicit gcd: start at max(a,b) and add the step until both divide. Same answer, slower in general.
#include <stdio.h>
int lcm_scan_positive(int a, int b) {
int step;
int m;
if (a <= 0 || b <= 0) {
return 0;
}
step = a > b ? a : b;
m = step;
while (m % a != 0 || m % b != 0) {
m += step;
}
return m;
}
int main(void) {
int number1 = 12;
int number2 = 18;
printf("LCM of %d and %d is: %d\n", number1, number2,
lcm_scan_positive(number1, number2));
return 0;
} Start at 18; it is not a multiple of 12. Add another 18 to reach 36, which both divide.
If a == 0 or b == 0, return 0 (this page’s convention).
Replace (a,b) with (b, a % b) until the remainder is 0.
Compute (a / g) * b in long long.
For 12 and 18, g = 6 and lcm is 36.
12 and 18Trace Euclid, then the divide-before-multiply formula.
| Step | a | b | Note |
|---|---|---|---|
1 | 12 | 18 | start Euclid |
2 | 18 | 12 | 12 % 18 = 12 after swap pattern |
3 | 12 | 6 | 18 % 12 = 6 |
4 | 6 | 0 | gcd = 6 |
5 | — | — | (12 / 6) * 18 = 36 |
Check: 12 · 18 = 216 and 216 / 6 = 36 — same lcm, but the formula avoids forming 216 in a narrow int first.
Where LCM thinking shows up beyond the interview prompt.
Two timers meet at the least common multiple of their cycles.
Example: events every 12 and 18 minutes meet at 36.
Common denominators often use lcm of the bottoms.
Example: 1/12 + 1/18 needs denominator 36.
Teaches divide-first and wider integer types in C.
Example: (long long)a / g * b.
Reduce an array by pairwise lcm.
Example: lcm(lcm(a,b), c).
If gcd = 1, then lcm = a * b (still widen the product).
Example: lcm(17, 13) = 221.
Reuse the same Euclid helper from the GCD tutorial.
Example: link both solutions in one interview answer.
Pro Tip: say the identity out loud — gcd · lcm = a · b — then show divide-first to prove you thought about overflow.
Why the gcd-based approach earns interview points.
The identity guarantees the least common multiple for nonnegative inputs.
Euclid is O(log min(a,b)); the formula itself is O(1).
One helper powers both GCD and LCM interview answers.
Divide-first plus long long shows production-minded C.
Pro Tip: mention that brute scan is fine for tiny demos but gcd is what you ship.
Small habits that keep LCM code clean in interviews.
Clarify the domain before coding; normalize signs if required.
Return 0 (or your documented convention) before dividing by gcd.
Use (a / g) * b, not (a * b) / g, in narrow integers.
Match long long results with the correct format specifier.
Cover the happy path, zero rule, and gcd = 1.
Pro Tip: dry-run 12 and 18 on paper (table above) before coding — it locks in both Euclid and the formula.
Mistakes that commonly break LCM solutions in C.
(a * b) / gcd can overflow even when the true lcm fits.
→ Use (a / g) * b and widen to long long.
Blindly dividing by gcd when an input is zero is messy and convention-dependent.
→ Return 0 early if either argument is zero (this page’s rule).
Stepping by max(a,b) can take a huge number of iterations.
→ Prefer the gcd formula; keep brute as a teaching demo.
Printing long long with %d is undefined behavior.
→ Use %lld (or cast carefully for the platform).
C remainder rules make naive Euclid messy on signed values.
→ Normalize with absolute values if the API promises a nonnegative lcm.
Check these inputs before calling the solution done.
(12, 18)gcd = 6, lcm = 36.
a == 0 or b == 0This page returns 0 for lcm; avoid dividing by a zero gcd path casually.
(17, 13)gcd = 1, so lcm = 17 * 13 = 221.
Repeatedly adding step can exceed INT_MAX; use wider counters for large inputs.
Fold: lcm(a,b,c) = lcm(lcm(a,b), c) (watch zeros at each step).
Normalize with absolute values if your API promises a nonnegative lcm.
Known results for common interview inputs.
(a, b) | gcd | lcm |
|---|---|---|
(12, 18) | 6 | 36 |
(4, 6) | 2 | 12 |
(17, 13) | 1 | 221 |
(0, 9) | 9 | 0 |
Try these variations to lock in the pattern.
scanf and validate nonnegative inputs%lld(a*b)/g vs (a/g)*b on large pairsa, b, gcd(a,b) · lcm(a,b) = a · b.a is always divisible by gcd(a,b), so a / g is an exact integer.lcm(0, n) = lcm(n, 0) = 0.Quick Takeaway: Euclid for gcd, then (a / g) * b in a wide type — and return 0 when either input is zero.
| Method | Time | Extra space |
|---|---|---|
| gcd + formula | O(log min(a,b)) | O(1) |
| Brute scan | O(lcm / max(a,b)) steps worst case | O(1) |
The gcd-based method dominates in practice.
LCM is the natural partner of GCD: one identity turns Euclid into a least-common-multiple function. Prefer the divide-first formula with a wide type, and keep the brute scan only as a definition check.
Practice both examples above, then continue to leap year for another classic branching warm-up.
Compute g = gcd(a,b), then return (a / g) * b in long long — or 0 if either input is zero.
long long (and %lld) for the resulta * b in int firstlong long with %dCompute them the interview-friendly way.
gcd · lcm = a · b
Definition(a / g) * b
Codelong long product
Safetylcm(0,·) = 0
EdgeO(log min)
AnalysisFor nonnegative integers a and b, gcd(a,b) · lcm(a,b) = a · b (with the convention lcm(a,0)=lcm(0,b)=0). That identity is why one small gcd loop unlocks lcm.
Learn how to check leap years with clear divisibility rules in C.
8 people found this page helpful