#include <stdio.h>
int gcd( int x, int y ); /* function prototype */
int main()
{
int j; /* loop counter */
int a; /* first number */
int b; /* second number */
/* loop for 5 pairs of inputs */
for ( j = 1; j <= 5; j++ ) {
printf( "Enter two integers: " );
scanf( "%d%d", &a, &b );
/* find greatest common divisor of a and b */
printf( "The greatest common divisor "
"of %d and %d is %d\n\n", a, b, gcd( a, b ) );
} /* end for */
return 0; /* indicate successful termination */
} /* end main */
/* gcd find greatest common divisor of x and y */
int gcd( int x, int y )
{
int i;
int greatest = 1; /* current gcd, 1 is minimum */
/* loop from 2 to smaller of x and y */
for ( i = 2; i <= ( ( x < y ) ? x : y ); i++ ) {
/* if current i divides both x and y */
if ( x % i == 0 && y % i == 0 ) {
greatest = i; /* update greatest common divisor */
} /* end if */
} /* end for */
return greatest; /* return greatest common divisor found */
} /* end function gcd */
求最大公约数
最新推荐文章于 2022-07-10 20:23:23 发布