求两个数的最大公约数c语言怎么做用while语句

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 06:08:31

求两个数的最大公约数c语言怎么做用while语句
求两个数的最大公约数c语言怎么做用while语句

求两个数的最大公约数c语言怎么做用while语句
int a,b,num1,num2,temp;
printf("Input a & 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); /*输出最小公倍数*/