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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niravkumar VaghelaPosted Jul 26, 2012, 8:34 AM
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....
Rahul BhattPosted Jul 26, 2012, 8:06 AM
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();