求两个数的最大公约数和最小公倍数

来源:互联网 发布:c语言读入txt文本文件 编辑:程序博客网 时间:2024/05/19 17:09


#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;}


0 0