How to calculate the LCM (Least Common Multiple) of two integers in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 12, b = 20;

            int lmc = (a > b) ? a : b;

            while (true)
            {
                if (lmc % a == 0 && lmc % b == 0)
                {
                    Console.WriteLine("The LCM (Least Common Multiple) of {0} and {1} is: {2}", a, b, lmc);
                    break;
                }
                lmc++;
            }
        }
    }
}

/*
run:
    
The LCM (Least Common Multiple) of 12 and 20 is: 60
   
*/

 



answered May 27, 2017 by avibootz

Related questions

1 answer 181 views
1 answer 176 views
1 answer 138 views
2 answers 237 views
2 answers 186 views
...