Least common multiple in C#

Calculate least common multiple
Least common multiple is the smallest positive integer that is divisible by both a and b. Now, if we know greatest common divisor of two positive, greater then zero integers a and b, we can calculate by using least common multiple formula:
Least-common-multiple

Least common multiple in C# is as follows:

static int GCD(int N, int M)
{

int R;

R = N % M;

while (R > 0)

N = M; M = R; R = N % M;

}return M;

}
static void Main(string[] args)
{

int A = 518;
int B = 392;
int C = GCD(A, B);
int lcm = A * B / C;
Console.WriteLine(“{0}”, lcm);
Console.ReadLine();

}
Least common multiple is used for fraction adding/subtracting.

External links:

Least common multiple on Wikipedia
Least common multiple on Mathsisfun
Least common multiple on Math.com

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*