hi,
can any one plz tl me the program for lcm and hcf of two numbers in c# programming.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rahul Bhatt165 reputationPosted Jul 26, 2012, 4:06 AM EDT
Using following method find lcm and hcf
FIND LCM
int lcf, number1, number2;
number1 = Convert.ToInt32(txtFirst.Text);
number2 = Convert.ToInt32(txtSecode.Text);
for (lcf = 1; ; lcf++)
{
if (lcf % number1 == 0 && lcf % number2 == 0)
{
txtThird.Text = lcf.ToString();
break;
}
}
FIND HCF
int number1, number2;
number1 = Convert.ToInt32(txtFirst.Text);
number2 = Convert.ToInt32(txtSecode.Text);
int hcf = 1, counter = 1;
// loop until the counter equals the smallest
// of the two numbers
while (counter <= Math.Min(number1, number2))
{
// if a and b divided by counter doesn't have a remainder
// we will update the HCF (because counter increments on each
// iteration)
if ((number1 % counter == 0) && (number2 % counter == 0))
{
hcf = counter;
}
// increment counter
counter++;
}
// return the highest common factor
txtFourth.Text = hcf.ToString();
Niravkumar Vaghela38 reputationPosted Jul 26, 2012, 4:34 AM EDT
static void Main(string[] args)
{
int num1 =10, num2=16, gcd, lcm, remainder, numerator, denominator;
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
Console.WriteLine("GCD of {0} and {1} = {2} \n", num1, num2, gcd);
Console.WriteLine("LCM of {0} and {1} = {2} \n", num1, num2, lcm);
Console.ReadLine();
}
I hope this will help you....