Advertisement
soxa

EuclideanAlgorithm

Nov 24th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using System;
  2. // Write a program that calculates the greatest common divisor (GCD) of given two numbers. Use the Euclidean algorithm (find it in Internet).
  3.  
  4. class GreatestDivisor
  5. {
  6.     static void Main()
  7.     {
  8.         int firstNum = int.Parse(Console.ReadLine());
  9.         int secNum = int.Parse(Console.ReadLine());
  10.  
  11.         int devider = 0;
  12.         int remainder = 0;
  13.         int saver = 0;//save secNum value
  14.  
  15.         while (true)
  16.         {
  17.             devider = firstNum / secNum;
  18.             remainder = devider * secNum;
  19.             saver = secNum;
  20.             secNum = firstNum - remainder;
  21.             firstNum = saver;
  22.  
  23.             if (secNum == 0)
  24.             {
  25.                 Console.WriteLine(saver);
  26.                 break;
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement