最大公约数和最小公倍数

来源:互联网 发布:电子图书馆软件 编辑:程序博客网 时间:2024/05/29 18:07

最大公约数和最小公倍数

#include <stdio.h>int main(){int a,b,num1,num2,temp;printf("Input a and b:");scanf("%d%d",&num1,&num2);if(num1>num2)//找出两个数中的较大值{temp=num1;//交换两个整数num1=num2;num2=temp;}a=num1;b=num2;while(b!=0)//采用辗转相除法求最大公约数{temp=a%b;a=b;b=temp;}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;}