[Find the least common multiple of two numbers in C language]

Problem analysis:
Enter two positive integers mn and find the least common multiple (LCM). For the least common multiple, it is understood that if there is a natural number a that can be divisible by the natural number b, then a is a multiple of b, and b is a divisor of a. For two positive integers, it refers to the smallest multiple of the two numbers. When calculating the least common multiple, there are two methods:
①Usually the greatest common divisor is used to assist the calculation.
Least common multiple = product of two numbers/largest common divisor. When solving a problem, first find the largest common divisor and then find it.

② Carry out algorithm design according to the definition. The least common multiple of any two positive integers is required to find the smallest natural number that can be divisible by two integers at the same time.
Idea:
First of all, for the two positive integers m and n entered, the order of each input may be different. Therefore, the size of the integer m and n must be compared and sorted, and the variable m stores large numbers and the variable n stores decimal numbers.

Secondly, find the least common multiple of the two numbers. If the large number m is a multiple of the decimal number n, then the large number m is the required number; if not, you can directly traverse from the large number m and increase backwards in turn until you find the first energy At the same time, the number is divisible by two numbers, so define a variable i, i starts from m, find the first natural number that can be divisible by two integers at the same time, and output it.
Note: After the
loop finds the first i value that satisfies the condition, the loop does not need to continue, so use break to end the loop.

The code is as follows:
#include<stdio.h>
int main()
{ int m, n, temp, i; printf("Input m & n:"); scanf("%d%d", &m, &n); if (m<n) / Compare the size so that m stores large numbers and n stores decimals / { temp = m; m = n; n = temp; } for(i=m; i>0; i++) / from big Start looking for natural numbers that meet the conditions / if(i%m










0 && i%n0)
{/ Output the natural numbers that meet the conditions and end the loop /
printf("The LCW of %d and %d is: %d\n", m, n, i);
break;
}
return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44436675/article/details/109792924