ellapt

6.8.GCD

Dec 18th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. using System;
  2. class GCD
  3. {
  4. static void Main()
  5. {
  6. string strNum1;
  7. string strNum2;
  8. double num1;
  9. double num2;
  10. double gcd;
  11. do
  12. {
  13. Console.Write("Please, enter a real number: ");
  14. }
  15. while ((!double.TryParse(strNum1 = Console.ReadLine(), out num1)) || num1 == 0 );
  16. do
  17. {
  18. Console.Write("Please, enter another real number: ");
  19. }
  20. while ((!double.TryParse(strNum2 = Console.ReadLine(), out num2)) || num2 == 0 );
  21. gcd = num1;
  22. if (Math.Abs(num1) < Math.Abs(num2))
  23. {
  24. num1 = num2;
  25. num2 = gcd;
  26. }
  27. gcd = num1 % num2;
  28. while (gcd!=0)
  29. {
  30. num1=num2;
  31. num2=gcd;
  32. gcd = num1 % num2;
  33. }
  34. gcd = num2;
  35. Console.WriteLine("GCD of {0} and {1} is {2}", strNum1, strNum2, Math.Abs(gcd));
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment