Print the least common multiplier (LCM) between two numbers in C#

 

Print the least common multiplier (LCM) between two numbers in C#

The least common multiple (LCM) of two integers A and B is the smallest integer that is a multiple of both A and B. For example, LCM(36, 84) = 252 because 252 is the smallest integer into which both 36 and 84 divide evenly.


C# code

public static void LCMNumber()

        {

            // LCM Number are Least Common Multiplication 

            // LCM of two number is the smallest number (not zero) that is a multiple of both. 

            // 6 = 2 * 3 

            // 8 = 2 * 2 * 2 

            // So the GCD of 6 and 8 is: 24 

            int num1, num2, a, b, lcmNum;

            Console.Write("Enter the First number: ");

            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the second number: ");

            num2 = Convert.ToInt32(Console.ReadLine());

            a = num2;

            b = num1;

            while (num1 != num2) // check num1 is equal or not 

            {

                if (num1 > num2) // check num1 is greater than num2 

                {

                    num1 = num1 - num2; // subtract num2 from num1 and save in num1 

                }

                else

                {

                    num2 = num2 - num1; // subtract num1 from num2 and save in num2 

                }

            }

            lcmNum = (a * b) / num1; // multiply the orinal two number and divide with num1 

            Console.Write("LCM number: " + lcmNum);

        }


Output



Share this

Related Posts

Previous
Next Post »