/*
 * euclid.c
 * This program reads two integer numbers and comuptes their greatest common
 * divisor (GCD) using Euclid's algorithm.
 */

#include <stdio.h>

int main ()
{
  int   a;
  int   b;
  int   temp;
  int   r;

  /* Read the two numbers. */
  printf ("Please enter two positive integers: ");
  scanf ("%d %d", &a, &b);

  /* Make sure that the input is legal. */
  if (a <= 0 || b <= 0)
  {
    printf ("The two numbers should be positive!\n");
    return (1);
  }

  /* Make sure that (a > b). */
  if (b > a)
  {
    temp = a;
    a = b;
    b = temp;
  }

  /* Perform Euclid's algorithm:
     Compute r = (a mod b).
     - If r == 0, then b is the GCD and we are done.
     - Otherwise, let a = b and b = r, and proceed for another iteration. */
  while ((r = a % b) != 0)
  {
    a = b;
    b = r;
  }

  /* Print the GCD. */
  printf ("The GCD is %d.\n", b);

  return (0);
}

