Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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).
- namespace _17.CalculateGCD
- {
- using System;
- public class EuclideanAlgorithm
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.Write("Enter a Integer number: ");
- int firstNum = int.Parse(Console.ReadLine());
- int secondNum;
- do
- {
- Console.Write("Enter other Integer number, different from the First: ");
- }
- while (!int.TryParse(Console.ReadLine(), out secondNum) || secondNum == firstNum);
- int numA = Math.Abs(firstNum);
- int numB = Math.Abs(secondNum);
- if (numB > numA)
- {
- int numC = numA;
- numA = numB;
- numB = numC;
- }
- while (numB > 0)
- {
- int remainder = numA % numB;
- numA = numB;
- numB = remainder;
- }
- int gcd = numA;
- Console.WriteLine("The Greatest Common Divisor of ({0}, {1}) is: {2} !", firstNum, secondNum, gcd);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement