// 輸入兩個正整數m和n, 求其最大公約數.
// 用輾轉相除法求最大公約數
// 演算法描述: m對n求餘為a, 若a不等於0 則 m
// 否則 n 為最大公約數
#include
int main()
{
int m, n; int m_cup, n_cup, res; /*被除數, 除數, 餘數*/
printf("Enter two integer:\n");
scanf("%d %d", &m, &n);
if (m > 0 && n >0)
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
m_cup = n_cup;
n_cup = res;
}
printf("最大公約數: %d\n", n_cup);
else
printf("Error!\n");
return 0;
// 輸入兩個正整數m和n, 求其最大公約數.
// 用輾轉相除法求最大公約數
// 演算法描述: m對n求餘為a, 若a不等於0 則 m
// 否則 n 為最大公約數
#include
int main()
{
int m, n; int m_cup, n_cup, res; /*被除數, 除數, 餘數*/
printf("Enter two integer:\n");
scanf("%d %d", &m, &n);
if (m > 0 && n >0)
{
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
{
m_cup = n_cup;
n_cup = res;
res = m_cup % n_cup;
}
printf("最大公約數: %d\n", n_cup);
}
else
{
printf("Error!\n");
}
return 0;
}