The greatest common divisor of two numbers

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42433334/article/details/102776773

Greatest common divisor

A common denominator:

Here Insert Picture Description

Second, the ideas:
  1、假定两数为a和b,通过a和b比较,将较小的数存放在变量min中;
  2、要求最大公约数,则需要从a,b中较小值开始递减遍历着找同时被a和b整除的数字,直到符合的数值为止;
Third, the code
#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a, b, min, i;
	scanf_s("%d %d", &a, &b);
	min = a;
	if (a>b)
		min = b;
	for (i = min; i > 0; --i)
	{
		if (a%i == 0 && b%i == 0)
		{
			printf("%d\n", i);
			break;
		}
	}
	system("pause");
	return 0;
}

Output:
Input: 8424
Output: 12Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42433334/article/details/102776773