求c语言程序。输入两个正整数m和n,求其最大公约数和最小公倍数。

如题所述

#include <stdio.h>
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("Greatest common divisor: %d\n", n_cup); 
printf("Lease common multiple : %d\n", m * n / n_cup); 

else printf("Error!\n"); 
return 0; 
}

辗转相除法求最大公约数

最小公倍数 = 两个数的积 / 最大公约数

温馨提示:内容为网友见解,仅供参考
第1个回答  2020-05-15
相似回答