legendxs

Euclid's Algorithm for GCD

Dec 12th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2.  
  3. class GCD
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Find the GCD (Greatest Common Denominator) of two numbers.");
  8.         Console.Write("Input first number: ");
  9.         string num1 = Console.ReadLine();
  10.         int x = Int32.Parse(num1);
  11.         Console.Write("Input second number: ");
  12.         string num2 = Console.ReadLine();
  13.         int y = Int32.Parse(num2);
  14.  
  15.         while (x != 0 && y != 0)
  16.         {
  17.             if (x > y)
  18.             {
  19.                 x %= y;
  20.             }
  21.             else
  22.             {
  23.                 y %= x;
  24.             }
  25.         }
  26.         int gcd = (int)Math.Max(x,y);
  27.         Console.WriteLine("The greatest common denominator of the numbers {0} and {1} is: {2}", num1, num2, gcd);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment