Least common multiple

Least common multiple

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.

Make


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

Use this boilerplate code as a starting point:

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 is divisible by both numbers with 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 lcd 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.

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Enter the first number: 2

Enter the second number: 4

The LCM of 2 and 4 is 4.

Enter the first number: 8

Enter the second number: 8

The LCM of 8 and 8 is 8.

Enter the first number: 6

Enter the second number: 4

The LCM of 6 and 4 is 12.

Enter the first number: 8

Enter the second number: 10

The LCM of 8 and 10 is 40.

Enter the first number: 16

Enter the second number: 72

The LCM of 16 and 72 is 144.

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.