The source code to find the LCM of two specified numbers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to find the LCM of two numbers.
using System;
class Demo
{
static void Main()
{
int firstNumber=0;
int secondNumber=0;
int temp1=0;
int temp2=0;
int lcm=0;
Console.Write("Enter the value of 1st number: ");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the value of 2nd number: ");
secondNumber = Convert.ToInt32(Console.ReadLine());
temp1 = firstNumber;
temp2 = secondNumber;
while (firstNumber != secondNumber)
{
if (firstNumber > secondNumber)
{
firstNumber = firstNumber - secondNumber;
}
else
{
secondNumber = secondNumber - firstNumber;
}
}
lcm = (temp1 * temp2) / firstNumber;
Console.WriteLine("Least Common Multiple is : " + lcm);
}
}
Output:
Enter the value of 1st number: 9
Enter the value of 2nd number: 15
Least Common Multiple is : 45
Press any key to continue . . .
Explanation:
Here, we created a class Demo that contains a Main() method. Here we found the LCM of two numbers. The LCM is the smallest number, which is the multiple of both numbers.
Console.Write("Enter the value of 1st number: ");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the value of 2nd number: ");
secondNumber = Convert.ToInt32(Console.ReadLine());
temp1 = firstNumber;
temp2 = secondNumber;
while (firstNumber != secondNumber)
{
if (firstNumber > secondNumber)
{
firstNumber = firstNumber - secondNumber;
}
else
{
secondNumber = secondNumber - firstNumber;
}
}
lcm = (temp1 * temp2) / firstNumber;
In the above code, we read the value of numbers and find the LCM of both numbers and then print the result on the console screen.
Program:
The source code to find the LCM of two specified numbers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
Here, we created a class Demo that contains a Main() method. Here we found the LCM of two numbers. The LCM is the smallest number, which is the multiple of both numbers.
In the above code, we read the value of numbers and find the LCM of both numbers and then print the result on the console screen.