題目:輸入兩個正整數(shù)m和n官辽,求其最大公約數(shù)和最小公倍數(shù)。
可以正確輸出的代碼:
#include <stdio.h>
main()
{
int n1,n2,temp,pro,d;
printf("Please enter two number:");
scanf("%d%d",&n1,&n2);
pro = n1 * n2;
if(n1 <= n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
while(n2 != 0)
{
d = n1 % n2;
n1 = n2;
n2 = d;
}
printf("The divisor is %d,and the muitiple is %d.\n",n1,pro / n1);
return 0;
}?
出錯的代碼:
#include <stdio.h>
main()
{
int n1,n2,temp,pro,d;
printf("Please enter two number:");
scanf("%d%d",&n1,&n2);
pro = n1 * n2;
if(n1 <= n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
while(d != 0)
{
d = n1 % n2;
n1 = n2;
n2 = d;
}
printf("The divisor is %d,and the muitiple is %d.\n",n1,pro / n1);
return 0;
}?
思考:為啥第二種代碼會出錯呢粟瞬?