您的位置:首页 > 其它

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

2013-05-09 23:18 337 查看
//输入两个正整数m和n,求其最大公约数和最小公倍数。
//程序分析:利用辗除法
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
int a = 0,b=0,num1=0,num2=0,temp=0;
printf("please input two numbers:\n");
scanf("%d,%d",&num1,&num2);
if(num1<num2)/*交换两个数,使大数放在num1上*/
{
temp=num1;
num1=num2;
num2=temp;
}
a = num1;
b = num2;
while(b != 0)/*利用辗除法,直到b为0为止*/
{
temp=a%b;
a=b;
b=temp;
}
printf("\r\n Greatest common divisor:%d",a);
printf("\r\n Least commoncmultiple:%d\n",num1*num2/a);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: