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;
}
}
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.