Author : Rahul

Write a program to calculate LCM of two numbers.


LCM

The LCM of two numbers is the smallest number which is fully divisible by both numbers. For example, the LCM of 20 and 30 is 60.

Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string firstNumberVal, secondNumberVal;
     int firstNumber, secondNumber, LCM, maxNumber;

     //User Input
     Console.Write("Enter First Number:");
     firstNumberVal = Console.ReadLine();
     firstNumber = Convert.ToInt32(firstNumberVal);
     Console.Write("Enter Second Number:");
     secondNumberVal = Console.ReadLine();
     secondNumber = Convert.ToInt32(secondNumberVal);
     
     //LCM Logic
     if(firstNumber > secondNumber)
     {
       maxNumber = firstNumber;
     }
     else
     {
       maxNumber = secondNumber;
     }
     while (true)
     {
       if (maxNumber % firstNumber == 0 && maxNumber % secondNumber == 0)
       {
         LCM = maxNumber;
         break;
       }
       maxNumber++;
     }
     
     //Print Result
     Console.WriteLine($"LCM of {firstNumber} and {secondNumber} is {LCM}.");
     
     Console.Read();
   }
 }
}

Output

Enter First Number:30
Enter Second Number:40
LCM of 30 and 40 is 120.