Enter the First Number : 2 Enter the Second Number : 4 Least Common Multiple : 4
This is a C# Program to find lcm.
This C# Program Finds and Display the L.C.M of a Given Number.
The least common multiple (LCM) of two numbers is the smallest number that is a multiple of both.
Here is source code of the C# Program to Find and Display the L.C.M of a Given Number. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Find and Display the L.C.M of a Given Number */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication9 { class Program { public static void Main(string[] args) { int num1, num2, x, y, lcm = 0; Console.Write("Enter the First Number : "); num1 = int.Parse(Console.ReadLine()); Console.Write("Enter the Second Number : "); num2 = int.Parse(Console.ReadLine()); x = num1; y = num2; while (num1 != num2) { if (num1 > num2) { num1 = num1 - num2; } else { num2 = num2 - num1; } } lcm = (x * y) / num1; Console.Write("Least Common Multiple is : " + lcm); Console.Read(); } } }
In this C# program, we are reading the First Number, Second Number using ‘num1’ and ‘num2’ variables respectively. Using while loop checks the value of ‘num1’ variable is greater than the value of ‘num2’ variable. If the condition is true then execute the statement.
If else condition statement is used to check the value of ‘num1’ variable is greater than the value of ‘num2’ variable. If the condition is true, then execute the statement. Compute the difference between the value of ‘num1’ and ‘num2’ variables.
Otherwise, if the condition is false, then execute the else statement. Compute the difference between the value of ‘num2’ variable by the value of ‘num1’ variable. Multiply the value of ‘x’ variable with the value of ‘y’ variable. Divide the resulted value by the value of ‘num1’ variable. Print the LCM of the number.
Enter the First Number : 2 Enter the Second Number : 4 Least Common Multiple : 4