how to calculate ratio in c# code example
Example: how to calculate ratio in c#
public string GetRatio(int a, int b)
{
var gcd = GCD(a, b);
return string.Format("{0}:{1}", a / gcd, b / gcd);
}
public int GCD(int a, int b)
{
return b == 0 ? Math.Abs(a) : GCD(b, a % b);
}
// Tomas Pajonk's Answer:
public int GCD_(int a, int b)
{
while (a != 0 && b != 0)
{
if (a > b)
{
a %= b;
}
else
{
b %= a;
}
}
if (a == 0)
{
return b;
}
else
{
return a;
}
}
public string GetRatio_(int a, int b)
{
var gcd_ = GCD_(a, b);
return string.Format("{0}:{1}", a / gcd_, b / gcd_);
}