code.club

標題: 求二數的最小公倍數 [打印本頁]

作者: enter    時間: 2015-5-8 17:57
標題: 求二數的最小公倍數
#include <stdio.h>

int main(void){

int x,y,i,j,lcm; //lcm 就是 lowest common multiple 最小公倍數  

printf("enter 2 numbers:\n");
scanf("%d %d",&x,&y);

if(x>y)
{i=x;
j=y;}
if(x<y)
{i=y;
j=x;}

for(lcm=i;;lcm++)
{
        if((lcm%i==0)&&(lcm%j==0)) break;
       
}

printf("The lcm of %d and %d is %d.",x,y,lcm);


return 0;
}





歡迎光臨 code.club (http://code.club/) Powered by Discuz! X3.2