版权声明:本文为博主原创文章,未经博主允许不得转载。
[plain] view plaincopy
- #include <stdio.h>
- int main()
- {
- int a, b, num1, num2, tmp;
- printf("Input a & b:");
- scanf("%d %d",&num1,&num2);
- if (num1 > num2) /*比较2个数,将较大的数与较小的数交换*/
- {
- tmp = num1;
- num1 = num2;
- num2 = tmp;
- }
- a = num1;
- b = num2;
- while (b != 0) /*用辗转相除法求最大公约数*/
- {
- tmp = a % b;
- a = b;
- b = tmp;
- }
- printf("The GCD of %d and %d is: %d\n", num1, num2, a); /*输出最大公约数*/
- printf("The LCM of them is: %d\n", num1*num2 / a); /*输出最小公倍数*/
- return 0;
- }