How to Calculate the LCM (Least Common Multiple) of Two Numbers in Python

import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

num1, num2 = 12, 18
lcm_result = lcm(num1, num2)
print(f"LCM of {num1} and {num2}: {lcm_result}")

Output:

LCM of 12 and 18: 36

Leave a Comment

Your email address will not be published.