C语言求最大公约数跟最小公倍数算法

C语言求最大公约数和最小公倍数算法


其算法过程为:前提设两数为a,b设其中a 做被除数,b做除数,temp为余数

1、大数放a中、小数放b中;

2、求a/b的余数;

3、若temp=0则b为最大公约数;

4、如果temp!=0则把b的值给a、temp的值给b;

5、返回第第二步;

#include <stdio.h>
#include <math.h>

void main(){
	
	int divisor(int a,int b);
	int multiple(int a,int b);
	printf("The highest common divisor is %d \n",divisor(15,9));
	printf("The lowest common multiple is %d \n",multiple(15,9));
}

int divisor(int a,int b){
	
	int temp;
	if(a<b){
		temp=a;a=b;b=temp;
	}
	while(b!=0)
	{
		temp = a%b;
		a=b;
		b=temp;
	}
	return (a);
}

int multiple(int a,int b){
	
	int divisor(int x,int y);
	int temp;
	temp = divisor(a,b);
	return a*b/temp;
}