Python Program - Find LCM

‮:sptth‬//www.theitroad.com

Here's a Python program to find the LCM (Least Common Multiple) of two numbers:

def lcm(a, b):
    # Find the greater number
    if a > b:
        greater = a
    else:
        greater = b

    while True:
        if greater % a == 0 and greater % b == 0:
            lcm = greater
            break
        greater += 1

    return lcm

# Example usage
num1 = 12
num2 = 16
print("The LCM of {} and {} is {}".format(num1, num2, lcm(num1, num2)))

Output:

The LCM of 12 and 16 is 48

In the above program, we first find the greater number among the two given numbers. Then we start a while loop and keep checking if the greater number is divisible by both the given numbers. If it is, then we have found the LCM and we break out of the loop. If not, we keep incrementing the greater number and check again. Finally, we return the LCM.