What is the LCM of two numbers?
★★★The lowest common multiple (LCM) is the lowest multiple shared by two or more numbers. For example, for the numbers 2 and 5, the lowest common multiple is 10 - the number both numbers can multiply into without a remainder.
Write a program that outputs the LCM of two numbers input.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
lcm that:Enter the first number: 2
Enter the second number: 5
The LCM of 2 and 5 is 10.
Enter the first number: 6
Enter the second number: 4
The LCM of 6 and 4 is 12.
Enter the first number: 7
Enter the second number: 40
The LCM of 7 and 40 is 280.
Enter the first number: 2
Enter the second number: 10
The LCM of 2 and 10 is 10.
# Least common multiple program
# -------------------------
# Subprograms
# -------------------------
# Function to use trial and error to calculate LCM
---
def lcm(number1, number2):
---
# Optimisation to start the counter at the highest number
if number1 > number2:
---
counter = number1
---
else:
---
counter = number2
---
# Find the LCM by checking every number until found
while counter % number1 != 0 or counter % number2 != 0:
---
counter = counter + 1
---
return counter
---
# -------------------------
# Main program
# -------------------------
---
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
---
result = lcm(number1, number2)
---
print("The LCM of {0} and {1} is {2}.".format(number1, number2, result))