Find the greatest common divisor of two numbers-the division method

Toss and toss division:
Toss and toss division, also known as Euclidean algorithm, is an algorithm for finding the greatest common divisor of two positive integers. Its specific method is: divide the larger number by the smaller number, then use the remaining number that appears (the first remainder) to remove the divisor, and then use the remaining number that appears (the second remainder) to remove the first remainder, and repeat, Until the last remainder is 0. If it is to find the greatest common divisor of two numbers, then the final divisor is the greatest common divisor of these two numbers.

The specific code is given below:

#include<stdio.h>
int main()
{
    
    
	int a, b, c;
	printf("请输入两个数:\n");
	scanf("%d %d", &a, &b);
	while (1){
    
    
		c = a%b;
		if (c == 0){
    
    
			break;
		}
		a = b;
		b = c;
		
	}
	printf("这两个数的最大公约数为:%d\n", b);
}

Guess you like

Origin blog.csdn.net/m0_52771278/article/details/110456339