Q. C# Program to Find LCM of two Integers.
Here you will find an algorithm and program in C# programming language to find LCM of two numbers. First let us understand what is LCM.
Explanation : LCM (Least Common Multiple) of numbers is defined as the smallest possible multiple of two or more numbers or we can say it is smallest number that is perfectly divisible by all the given number without a remainder.
For Example : LCM of 10 and 20 is 20. 20 is the least number which can perfectly divide both 10 and 20. Therefore LCM of 10 and 20 is 20.
Algorithm to find LCM of two numbers
START Step 1 → Initialize num1 and num2 with positive integers Step 2 → Store maximum of num1 & num2 to max Step 3 → Check if max is divisible by num1 and num2 Step 4 → If divisible, Display max as LCM Step 5 → If not divisible then increase max by 1 and then goto step 3 STOP
C# Program to Find LCM of two Numbers
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication9 { class LFC { public static void Main(string[] args) { int num1 = 10, num2 = 20, max, lcm; max = (num1>num2) ? num1 : num2; for(i=max; ; i+=max) { if(i%num1==0 && i%num2==0) { lcm = i; break; } } Console.Write("\nLCM of {0} and {1} = {2}\n\n", num1, num2, lcm); } } }
Output
LCM of 10 and 20 is 20