C语言程序纠错,麻烦大家看看

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:25:57
下面是求两个数的最大公约数和最小公倍数的算法,为什么当我输入的第一个数比第二个数大时,程序运行出错?
#include<stdio.h>
main()
{
int m,n,t,i;
printf("Please enter two positive integers: ");
scanf("%d%d",&m,&n);
if(m<n)
{
t=m*n;
while(m!=0)
{
i=n%m;
n=m;
m=i;
}
printf("Greatest common divisor:%d\n",n);
printf("Least common multiple:%d\n",t/n);
}
else
{
t=m*n;
while(n!=0)
{
i=m%n;
m=n;
n=i;
}
printf("Greatest common divisor:%d\n",n);
printf("Least common multiple:%d\n",t/n);
}
}

#include<stdio.h>
main()
{
int m,n,t,i;
printf("Please enter two positive integers: ");
scanf("%d%d",&m,&n);
if(m<n)
{
t=m*n;
while(m!=0)
{
i=n%m;
n=m;
m=i;
}
printf("Greatest common divisor:%d\n",n);
printf("Least common multiple:%d\n",t/n);
}
else
{
t=m*n;
while(n!=0)
{
i=m%n;
m=n;
n=i;
}
printf("Greatest common divisor:%d\n",m); /*这里是m*/
printf("Least common multiple:%d\n",t/m); /*这里是除以m,n在循环退出时已经是0了,t除以0的话就出现错误啦*/
}
}

没错啊!