In this example, you will learn how to Find the LCM value of two numbers entered by user in C# Console.
Pseudocode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
BEGIN NUMBER n1 , n2 , lcm; OUTPUT "Enter first Number:"; INPUT n1 OUTPUT "Enter second Number:"; INPUT n2 lcm = (n1 > n2) ? n1 : n2; WHILE (true) THEN IF (lcm % n1 == 0 && lcm % n2 == 0) THEN OUTPUT "The LCM of "+n1+"and "+n2+" is "+ lcm; BREAK WHILE; END IF ++lcm; END WHILE END |
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Program { static void Main(string[] args) { int n1 , n2 , lcm; Console.Write("Enter firstNumber:"); n1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter firstNumber:"); n2 = Convert.ToInt32(Console.ReadLine()); // maximum number between n1 and n2 is stored in lcm lcm = (n1 > n2) ? n1 : n2; // Always true while (true) { if (lcm % n1 == 0 && lcm % n2 == 0) { Console.WriteLine("The LCM of {0} and {1} is {2}", n1, n2, lcm); break; } ++lcm; } Console.ReadKey(); } } |
Output: