CHAWTeam
請撰寫一程式,輸入二個正整數,找出兩整數的公因數與最大公因數。
格式如輸出範例所示,公因數中間以空格隔開。
輸入範例:
30 60
輸出範例:
30與60的公因數1 2 3 5 6 10 15 30
30與60的最大公因數30
#include <stdio.h>
int main()
{
int a, b, max;
scanf("%d %d", &a, &b);
printf("%d與%d的公因數", a, b);
for (int i = 1; a >= i && b >= i; i++)
{
if (a % i == 0 && b % i == 0)
{
printf("%d ", i);
max = i;
}
}
printf("\n%d與%d的最大公因數%d\n", a, b, max);
return 0;
}
30 60
30與60的公因數1 2 3 5 6 10 15 30
30與60的最大公因數30
查看我們在HackMD上的所有筆記
目錄:DICE C++語言程式破解