# Euclid's algorithm

最大公約数を求めるアルゴリズム

C
#include <stdio.h>

int bcd(int m, int n);
main()
{
  int m, n, temp;
  printf("Type in the first integer.\n");
  scanf("%d", &m);

  printf("Type in the second integer.\n");
  scanf("%d", &n);

  if (m<=0 || n<=0) {
    printf("Invalid number.\n");
    exit(1);
  }
  
  if (m < n) {
    temp = m;
    m = n;
    n = temp;
  }  
  
  printf("GCD = %d\n", gcd(m, n));  
}

int gcd(int m, int n)
{
  int a, b, temp;
  a = m;
  b = n;
  while (b != 0) {
    temp = a%b;
    a = b;
    b = temp;
  }
  return(a);
}