C語言練習題
從鍵盤輸入兩個數m和n,求其最大公約數和最小公倍數。
實現方法如下:
main()
{
int m,n,a,b;
int p,r,temp;
printf("Please input 2 integer(m,n):");
scanf("%d,%d",&m,&n);
a = m; b = n;
if(b<a)
{
temp = a;
a = b;
b = temp;
}
p = a * b;
while(a != 0)
{
r = b % a;
b = a;
a = r;
}
printf("The Greatest Common Divisor %d and %d is: %d/n",m,n,b);
printf("The Least Common Multiple %d and %d is: %d/n",m,n,p/b);
}
運行結果為:
新聞熱點
疑難解答