题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

来源:互联网 发布:mac disk0s1 编辑:程序博客网 时间:2024/06/06 04:09

程序分析:利用辗除法。

#include <stdio.h>

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;
}