Tristan M. - 8 months ago 57

C# Question

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;

}

}

Answer

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++)
{
if ((num1 * i) % num2 == 0)
{
return i * num1;
}
}
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
```

Source (Stackoverflow)