Friday, March 26, 2010

Get L.C.M (Least Common Multiple) of two numbers in C#

open a console application project in c# language in your editor and just paste the following code in your class's brackets -


static void Main(string[] args) {

int tempNum, number1, number2;
Console.Write("Enter First Number: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Enter Secnd Number: "); number2 = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
for (tempNum = 1; ; tempNum++)
{
if (tempNum % number1 == 0 && tempNum % number2 == 0)
{
Console.WriteLine("L.C.M is - "); Console.WriteLine(tempNum.ToString());
Console.Read();
break;
}
}
}

Output is -

2 comments: