Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

HCF and LCM in C#

The following code explains the calculation of HCF and LCM in C#.

  1. using System;
  2. using System.Text;
  3.  
  4. namespace forgetCode
  5. {
  6.  
  7. class program
  8. {
  9. public static void Main()
  10. {
  11. int a, b, x, y, t, gcd, lcm;
  12. Console.WriteLine("Enter two integers\n");
  13. x=Convert.ToInt32(Console.ReadLine());
  14. y = Convert.ToInt32(Console.ReadLine());
  15.  
  16. a = x;
  17. b = y;
  18.  
  19. while (b != 0)
  20. {
  21. t = b;
  22. b = a % b;
  23. a = t;
  24. }
  25.  
  26. gcd = a;
  27. lcm = (x * y) / gcd;
  28. Console.WriteLine("Greatest common divisor of {0} and {1}= {2}\n", x, y, gcd);
  29. Console.WriteLine("Least common multiple of{0} and {1}= {2}\n", x, y, lcm);
  30.  
  31. }
  32. }
  33. }


Output:
Enter two integers

8
12
Greatest common divisor of 8 and 12= 4

Least common multiple of8 and 12= 24