C language realizes the least common multiple and the greatest common divisor of two integers

//求最小公倍数算法:最小公倍数=两整数的乘积÷最大公约数
//(1)辗转相除法
//有两整数a和b:
//① a%b得余数c
//② 若c=0,则b即为两数的最大公约数
//③ 若c≠0,则a=b,b=c,再回去执行①
#include<stdio.h>
int main()   // 辗转相除法求最大公约数 
{ 
   int s, a, b, c;
   printf("Input two integer numbers:\n");
   scanf("%d%d", &a, &b);
   s=a*b;
   c=a%b;
   while(c!=0)  // 余数不为0,继续相除,直到余数为0 
   { 
	  a=b;  b=c; c=a%b;
   }
   printf("最大公倍数:%d\n", b);
   printf("最小公倍数:%d\n", s/b);
}

 

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/106555662