KlimentHristov

CalculateGCD

Dec 2nd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. class CalculateGCD
  3. {
  4.     // CONDITION:
  5.     // Write a program that calculates the greatest common divisor (GCD) of given two integers a and b. Use the Euclidean algorithm (find it in Internet).
  6.  
  7.     static void Main()
  8.     {
  9.         int a = int.Parse(Console.ReadLine());
  10.         int b = int.Parse(Console.ReadLine());
  11.         int sum;
  12.  
  13.         if (a != 0 && b != 0)
  14.         {
  15.             if (a < b)
  16.             {
  17.                 sum = b - a;
  18.                 a = a - sum - sum;
  19.                 if (a == 0)
  20.                 {
  21.                     Console.WriteLine(sum);
  22.                 }
  23.                 else
  24.                 {
  25.                     if ((b %= a) == 0)
  26.                     {
  27.                         Console.WriteLine(a);
  28.                     }
  29.                     else if ((a %= b) == 0)
  30.                     {
  31.                         Console.WriteLine(b);
  32.                     }
  33.                 }
  34.             }
  35.             else if (a > b)
  36.             {
  37.                 sum = a - b;
  38.                 b = b - sum - sum;
  39.                 if (b == 0)
  40.                 {
  41.                     Console.WriteLine(sum);
  42.                 }
  43.                 else
  44.                 {
  45.                     if ((a %= b) == 0)
  46.                     {
  47.                         Console.WriteLine(b);
  48.                     }
  49.                     else if ((b %= a) == 0)
  50.                     {
  51.                         Console.WriteLine(a);
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.        
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment