代码文本:
#include "stdio.h"
int mylcm(int a,int b){//求LCM函数
int t;
if(a
t=a,a=b,b=t;
for(t=a;t%b;t+=a);
return t;//返回LCM
}
int main(int argc,char *argv[]){
int a,b;
printf("Enter a & b(int a>0,b>0)...\n");
if(scanf("%d%d",&a,&b)==2 && a>0 && b>0)
printf("The LCM is %d\n",mylcm(a,b));
else//提示输入错误并退出
printf("Input error, exit...\n");
return 0;
}
比较简单,供参考……
#include
int gcd(int a,int b)
{ int r;
while(r=a%b)a=b,b=r;
return b;
}
int main()
{ int a,b;
scanf("%d%d",&a,&b);
if(a<1||b<1)printf("Input error!\n");
else printf("%d\n",a*b/gcd(a,b));
return 0;
}