Advertisement
Guest User

SoftUni_Homework_1_6

a guest
Aug 17th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SoftUni_Homework_1_6
  7. {
  8. class Program
  9. {
  10. /// <summary>
  11. /// Get the Greatest Common Divisor
  12. /// </summary>
  13. /// <param name="num1">first number</param>
  14. /// <param name="num2">second number</param>
  15. /// <returns> int value of GCD </returns>
  16. static int GetGCD(int num1, int num2)
  17. {
  18. // Compare the two numbers and assigning the difference to the larger number until the two numbers are equal.
  19. while (num1 != num2)
  20. {
  21. if (num1 > num2)
  22. {
  23. num1 = num1 - num2;
  24. }
  25. if (num2 > num1)
  26. {
  27. num2 = num2 - num1;
  28. }
  29. }
  30. return num1;
  31. }
  32.  
  33. /// <summary>
  34. /// Get the Least Common Multiple
  35. /// </summary>
  36. /// <param name="num1">first number</param>
  37. /// <param name="num2">second number</param>
  38. /// <returns> int value of LCM </returns>
  39. static int GetLCM(int num1, int num2)
  40. {
  41. // Get the LCM from the product of the numbers divided by the GCD
  42. return (num1 * num2) / GetGCD(num1, num2);
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47. Console.WriteLine("LCM of {0},{1} is {2}", 1234, 3456, GetLCM(1234, 3456));
  48. Console.ReadKey();
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement