辗转相除法求最大公约数

代码

#include <stdio.h>
int main()
{
	int a,b,r,temp;
	printf("Please enter a ,b:");
	scanf("%d,%d",&a,&b);
	if(a<b)
	{
		temp=a;
		a=b;
		b=temp;
	}
	r=a%b;
	while(r!=0)
	{
		a=b;
		b=r;
		r=a%b;
	}
	//当r=0时,b的值就是最大公约数
	printf("%d\n",b);
	return 0;
}

截图

posted @ 2022-12-22 09:12  戴骏  阅读(31)  评论(0编辑  收藏  举报