Calculating the Least Common Multiple (LCM)
The following code demonstrates how to compute the Least Common Multiple (LCM) of two numbers in Python:
Example
# Define a function def lcm(x, y): """This function returns the Least Common Multiple of two numbers""" # Get the greater of the two values if x > y: greater = x else: greater = y while(True): if (greater % x == 0) and (greater % y == 0): lcm = greater break greater += 1 return lcm # Get user input num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) print(num1, "and", num2, "have a Least Common Multiple of", lcm(num1, num2))
Example Output:
Enter the first number: 54 Enter the second number: 24 54 and 24 have a Least Common Multiple of 216