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.

Least common multiple

Make

Write a program that outputs the LCM of two numbers input.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Create a subprogram called lcm that:

  1. Takes two parameters: number1 and number2.
  2. Starts a counter at the highest of the two numbers.
  3. Counts up until the counter divided by either number has no remainder.
  4. Returns the counter.

Complete the main program so that:

  1. The user can input two numbers.
  2. The result is calculated by calling the subprogram lcm.
  3. The result is output in the format: “The LCM of {number1} and {number2} is {result}”.

Typical inputs and outputs from the program would be:

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.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# 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))