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:The LCM of {number1} and {number2} is {result}.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.
// 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}.");
---
}
---
}