This post shows how to find the LCM (Least Common Multiple) of two numbers in Python — the smallest number that is a multiple of both numbers.
Find LCM of two numbers in Python:
Explanation:
There's a direct relationship between GCD and LCM: multiplying two numbers always equals their GCD times their LCM. So once we have the GCD, we can compute the LCM as (a * b) // gcd.
Python Program to Find LCM of Two Numbers
def find_gcd(x, y):
while y:
x, y = y, x % y
return x
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
gcd = find_gcd(a, b)
lcm = (a * b) // gcd
print(f"The LCM is {lcm}")
Output:
Enter first number: 4
Enter second number: 6
The LCM is 12
GCD and LCM questions are common interview warm-ups — see more in our Interview Prep section.
