Find LCM in Java
What you’ll learn
- How to compute LCM using gcd formula.
- How to implement Euclidean gcd in Java.
- A brute-scan approach for concept clarity.
Prerequisites
- Basic Java syntax, loops, conditionals, and methods.
- Comfort with integer arithmetic and console input/output.
Math definition
This problem is driven by a deterministic numeric rule: for each valid input, apply the same formula repeatedly and classify the result.
Intuition with examples
Work a small input by hand first, then map each step to one Java loop iteration. This makes the control flow and stopping condition clear.
Live preview
Algorithm
Goal: compute lcm(a,b) safely for integer inputs.
Find gcd with Euclid
Iterate (a, b) = (b, a % b) until b == 0; the remaining a is gcd.
Compute lcm from gcd
If either input is zero return 0; else use (a / gcd) * b in a wider type.
📜 Pseudocode
function gcd(a, b):
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) * bLCM from gcd (12, 18)
public class Main {
static int findGcd(int num1, int num2) {
num1 = Math.abs(num1);
num2 = Math.abs(num2);
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
static long findLcmLong(int num1, int num2) {
if (num1 == 0 || num2 == 0) return 0L;
int g = findGcd(num1, num2);
return (long) (num1 / g) * (long) num2;
}
public static void main(String[] args) {
int number1 = 12;
int number2 = 18;
long lcm = findLcmLong(number1, number2);
System.out.println("LCM of " + number1 + " and " + number2 + " is: " + lcm);
}
}LCM of 12 and 18 is: 36
Brute scan approach
public class Main {
static int lcmScanPositive(int a, int b) {
if (a <= 0 || b <= 0) return 0;
int step = Math.max(a, b);
int m = step;
while (m % a != 0 || m % b != 0) {
m += step;
}
return m;
}
public static void main(String[] args) {
int number1 = 12;
int number2 = 18;
System.out.println("LCM of " + number1 + " and " + number2 + " is: " + lcmScanPositive(number1, number2));
}
}LCM of 12 and 18 is: 36
Optimization notes
Keep it simple. Prefer clear loops and method extraction before micro-optimizations.
Reuse computed values. Avoid recomputing the same sub-result inside nested loops.
❓ FAQ
🔄 Input / output examples
Use the sample programs as reference: provide valid numeric input and verify the output pattern matches the problem definition.
| (a, b) | gcd | lcm |
|---|---|---|
(12, 18) | 6 | 36 |
(4, 6) | 2 | 12 |
(17, 13) | 1 | 221 |
(0, 9) | 9 | 0 |
Edge cases
Smallest valid input
Confirm behavior for minimum accepted values.
Invalid input
Guard against non-numeric or out-of-range values when input is user-provided.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| gcd + formula | O(log min(a,b)) | O(1) |
| Brute scan | O(lcm / max(a,b)) in worst case | O(1) |
Summary
- Use a direct Java implementation of the numeric rule.
- Validate input and test boundary cases.
- Discuss complexity clearly in interviews.
For nonnegative integers a and b, gcd(a,b) * lcm(a,b) = a * b. This identity helps compute lcm quickly once gcd is known.
8 people found this page helpful
