Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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:
7
Enter the second number:
40
The LCM of 7 and 40 is 280.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Least common multiple program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Function to use trial and error to calculate LCM
---
    static int lcm(int number1, int number2)
    {
---
        int counter;
        // 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 || counter % number2 != 0)
        {
---
            counter++;
---
        }
---
        return counter;
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the first number:");
        int number1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter the second number:");
        int number2 = Convert.ToInt32(Console.ReadLine());
---
        int result = lcm(number1, number2);
        Console.WriteLine($"The LCM of {number1} and {number2} is {result}.");
---
    }
---
}