Advertisement
remote87

GCM

Sep 9th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _17.CalculateGCD
  8. {
  9.     class CalculateGCD
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter a number a: ");
  14.             int a = int.Parse(Console.ReadLine());
  15.             Console.Write("Enter a number b: ");
  16.             int b = int.Parse(Console.ReadLine());
  17.             int gcd = 1;
  18.  
  19.             int tempA = 0;
  20.             int tempB = 0;
  21.             Console.WriteLine("The prime divisors of a are: ");
  22.             for (int i = a; i >= 1; i--)
  23.             {
  24.                 if (a % i == 0)
  25.                 {
  26.                     tempA = a / i;
  27.                 }
  28.                 else
  29.                 {
  30.                     continue;
  31.                 }
  32.                 Console.Write("{0} ", tempA);
  33.             }
  34.             Console.WriteLine();
  35.  
  36.             Console.WriteLine("The prime divisors of b are: ");
  37.             for (int j = b; j >= 1; j--)
  38.             {
  39.                 if (b % j == 0)
  40.                 {
  41.                     tempB = b / j;
  42.                 }
  43.                 else
  44.                 {
  45.                     continue;
  46.                 }
  47.                 Console.Write("{0} ", tempB);
  48.             }
  49.             Console.WriteLine();
  50.             if (a > b)
  51.             {
  52.                 for (gcd = b; gcd >= 1 ; gcd--)
  53.                 {
  54.                     if ((a % gcd == 0) && (b % gcd) == 0)
  55.                     {
  56.                         Console.WriteLine("Greatest divisor of a and b: {0}.", gcd);
  57.                         break;
  58.                     }
  59.                     else
  60.                     {
  61.                         continue;
  62.                     }
  63.                 }
  64.             }
  65.             if (b > a)
  66.             {
  67.                 for (gcd = a; gcd >= 1 ; gcd--)
  68.                 {
  69.                     if ((b % gcd == 0) && (a % gcd) == 0)
  70.                     {
  71.                         Console.WriteLine("Greatest divisor of a and b: {0}.", gcd);
  72.                         break;
  73.                     }
  74.                     else
  75.                     {
  76.                         continue;
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement