Python program that finds the least common multiple (LCM) of two given numbers:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Initialize variables
lcm = 0
# Find the maximum number
if num1 > num2:
max_num = num1
else:
max_num = num2
# Find the LCM
while True:
if max_num % num1 == 0 and max_num % num2 == 0:
lcm = max_num
break
max_num += 1
print("The LCM of", num1, "and", num2, "is", lcm)
The program prompts the user to enter two numbers using the input() function, and stores the values in the variables “num1” and “num2”. It then uses an if-else statement to find the maximum number out of the two numbers. Then it uses a while loop to iterate through the range of numbers starting from the maximum number, until it finds the least common multiple of the two numbers. If the current number is divisible by both the numbers, it assigns the current number to the variable “lcm” and breaks out of the loop. At the end of the loop, it prints the LCM of the two numbers.
Another way to find the LCM of two numbers is by using the built-in math function gcd()
and the mathematical formula LCM.