# C# Program to Find LCM of Two Numbers

«
»

This is a C# Program to find lcm.

Problem Description

This C# Program Finds and Display the L.C.M of a Given Number.

Problem Solution

The least common multiple (LCM) of two numbers is the smallest number that is a multiple of both.

Program/Source Code

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 : ");
Console.Write("Enter the Second Number : ");
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);
}
}
}```
Program Explanation

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.

Runtime Test Cases
```
Enter the First Number : 2
Enter the Second Number : 4
Least Common Multiple : 4```

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs. 