How to find HCF/GCD with C#?
using System;
public class Test
{
private static int HCF(int x, int y)
{
if (0 == x) return y;
if (0 == y) return x;
if (x == y) return x;
if (x < y) return HCF(x, y-x);
return HCF(x-y, y);
}
private static int GCD(int x, int y)
{
if (0 == y) return x;
return GCD(y, x % y);
}
public static void Main(string[]args)
{
int x = 14, y = 18;
Console.WriteLine("HCF of {0} & {1} is {2}", x, y, HCF(x, y));
Console.WriteLine("GCD of {0} & {1} is {2}", x, y, GCD(x, y));
}
}
Comments
Post a Comment