2011年10月16日 星期日

C 輾轉相除法練習 (GCD)

#include <stdio.h>
#include <stdlib.h>

int GCD(int big, int small)
{
  int remainder = 1;
  while(remainder !=0)
   {

      remainder = big % small;
      big = small;
      small = remainder;      

   }
   return big;
}

int main()
{
  int a,b;
  int rval;
  printf("please input two number\t"); 
  scanf("%d %d",&a,&b);
    rval = GCD(a,b);
 
  printf("The GCD is %d",rval);
  system("pause");   
}

沒有留言:

張貼留言