I have the current coding which used to be a goto but I was told to not use goto anymore as it is frowned upon. I am having troubles changing it into for say a while loop. I am fairly new to C# and programming in general so some of this is completely new stuff to me. Any help would be appreciated. The actual question is input two numbers and find the lowest common multiple.
Here is the original with goto:
BOB:
if (b < d)
{
a++;
myInt = myInt * a;
b = myInt;
myInt = myInt / a;
if (b % myInt2 == 0)
{
Console.Write("{0} ", h);
Console.ReadLine();
}
}
if (d < b)
{
c++;
myInt2 = myInt2 * c;
d = myInt2;
myInt2 = myInt2 / c;
if (d % myInt == 0)
{
Console.Write("{0} ", t);
Console.ReadLine();
}
else
{
goto BOB;
}
}
else
{
goto BOB;
}
}
The LCM of 2 and 4 is 4. To find the least common multiple of 2 and 4, we need to find the multiples of 2 and 4 (multiples of 2 = 2, 4, 6, 8; multiples of 4 = 4, 8, 12, 16) and choose the smallest multiple that is exactly divisible by 2 and 4, i.e., 4.
LCM denotes the least common factor or multiple of any two or more given integers. For example, L.C.M of 16 and 20 will be 2 x 2 x 2 x 2 x 5 = 80, where 80 is the smallest common multiple for numbers 16 and 20.
LCM of 4 and 10 is 20. The Least Common multiple or Lowest common multiple simply known as LCM is the smallest or the least positive integer that is divisible by the given set of numbers. In the given set of numbers 4 and 10, 20 is the first(least or smallest) number that is common in the set of multiples of 4 and 10.
Here's a more efficient and concise implementation of the Least Common Multiple calculation which takes advantage of its relationship with the Greatest Common Factor (aka Greatest Common Divisor). This Greatest Common Factor function uses Euclid's Algorithm which is more efficient than the solutions offered by user1211929 or Tilak.
static int gcf(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } static int lcm(int a, int b) { return (a / gcf(a, b)) * b; }
For more information see the Wikipedia articles on computing LCM and GCF.
Try This:
using System;
public class FindLCM
{
public static int determineLCM(int a, int b)
{
int num1, num2;
if (a > b)
{
num1 = a; num2 = b;
}
else
{
num1 = b; num2 = a;
}
for (int i = 1; i < num2; i++)
{
int mult = num1 * i;
if (mult % num2 == 0)
{
return mult;
}
}
return num1 * num2;
}
public static void Main(String[] args)
{
int n1, n2;
Console.WriteLine("Enter 2 numbers to find LCM");
n1 = int.Parse(Console.ReadLine());
n2 = int.Parse(Console.ReadLine());
int result = determineLCM(n1, n2);
Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
Console.Read();
}
}
Output:
Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24
Try this
int number1 = 20;
int number2 = 30;
for (tempNum = 1; ; tempNum++)
{
if (tempNum % number1 == 0 && tempNum % number2 == 0)
{
Console.WriteLine("L.C.M is - ");
Console.WriteLine(tempNum.ToString());
Console.Read();
break;
}
}
// output -> L.C.M is - 60
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With