The Program in C# Program to find LCM of two numbers using HCF is given below:
using System;
namespace LCM_HCF
{
class Program
{
static void Main(string[] args)
{
int num1, num2, hcf, lcm;
Console.WriteLine("Hello Codeauri Family,enter first and second numbers below to find lcm : ");
Console.Write("Well,enter the first number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Well, enter the second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
hcf = HCF(num1, num2);
lcm = num1 * num2 / hcf;
Console.WriteLine("Okay, The LCM of " + num1 + " and " + num2 + " is " + lcm);
Console.ReadLine();
}
static int HCF(int n1, int n2)
{
int remainder;
while (n2 != 0)
{
remainder = n1 % n2;
n1 = n2;
n2 = remainder;
}
return n1;
}
}
}
Output:
Hello Codeauri Family,enter first and second numbers below to find lcm :
Well,enter the first number: 5
Well, enter the second number: 10
Okay, The LCM of 5 and 10 is 10
Pro-Tips💡
Here are the step by step execution of above program:
using System;
– This line is a using directive, which tells the compiler to use theSystem
namespace, which includes classes and methods used in the program.namespace LCM_HCF
– The namespace declaration creates a namespace namedLCM_HCF
to group classes and other code elements in a logical and organized manner.class Program
– This is a class declaration, creating a class namedProgram
.static void Main(string[] args)
– This is the main method of the program, the entry point for the program. When the program is executed, the code inside theMain
method is executed first.int num1, num2, hcf, lcm;
– This line declares four integer variables:num1
,num2
,hcf
, andlcm
.Console.WriteLine("Hello Codeauri Family,enter first and second numbers below to find lcm : ");
– This line writes a message to the console asking the user to enter two numbers.Console.Write("Well,enter the first number: ");
– This line writes a message to the console asking the user to enter the first number.num1 = Convert.ToInt32(Console.ReadLine());
– This line reads the first number entered by the user, converts it to an integer using theConvert.ToInt32
method, and assigns it to thenum1
variable.Console.Write("Well, enter the second number: ");
– This line writes a message to the console asking the user to enter the second number.num2 = Convert.ToInt32(Console.ReadLine());
– This line reads the second number entered by the user, converts it to an integer using theConvert.ToInt32
method, and assigns it to thenum2
variable.hcf = HCF(num1, num2);
– This line calls theHCF
method and assigns the result to thehcf
variable.lcm = num1 * num2 / hcf;
– This line calculates the LCM by multiplyingnum1
andnum2
and dividing the result by thehcf
. The result is assigned to thelcm
variable.Console.WriteLine("Okay, The LCM of " + num1 + " and " + num2 + " is " + lcm);
– This line writes the result to the console.Console.ReadLine();
– This line waits for the user to press enter before ending the program.static int HCF(int n1, int n2)
– This method calculates the HCF of two numbers.int remainder;
– This line declares an integer variableremainder
.while (n2 != 0)
– This line starts awhile
loop that will run as long asn2
is not equal to 0.remainder = n1 % n2;
– This line calculates the remainder whenn1
is divided
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.