Home  >  Article  >  How to find the least common multiple in C language

How to find the least common multiple in C language

zbt
zbtOriginal
2023-09-28 10:41:442595browse

Least Common Multiple Multiple (LCM for short) refers to the smallest positive integer among two or more numbers that can be divided by each number. In mathematics, finding the least common multiple is a common problem, and in programming, we can use C language to solve this problem.

In C language, we can use loops and conditional statements to solve for the least common multiple. The following is an example of a C language program that solves the least common multiple:

#include
int findLCM(int num1, int num2) {
int max, lcm;
// 选择两个数中的较大数
max = (num1 > num2) ? num1 : num2;
// 循环判断最小公倍数
while (1) {
if (max % num1 == 0 && max % num2 == 0) {
lcm = max;
break;
}
max++;
}
return lcm;
}
int main() {
int num1, num2, lcm;
printf("请输入两个正整数:");
scanf("%d %d", &num1, &num2);
lcm = findLCM(num1, num2);
printf("最小公倍数为:%d\n", lcm);
return 0;
}

In the above program, we define a function named findLCM to solve for the least common multiple. This function accepts two parameters num1 and num2, which represent two positive integers respectively. In the function, we first select the larger of the two numbers as the initial value, and then use a while loop to determine the least common multiple. In the loop, we use conditional statements to determine whether the current max is divisible by both num1 and num2. If so, it means that the least common multiple has been found, assign it to lcm and jump out of the loop. If not, add 1 to max and continue to judge the next number.

In the main function, we first use the printf function to prompt the user to enter two positive integers, and then use the scanf function to assign the values ​​entered by the user to num1 and num2 respectively. Next, we call the findLCM function, passing in num1 and num2 as parameters, solve for the least common multiple, and assign the result to lcm. Finally, we use the printf function to output the value of the least common multiple.

By running the above program, we can get the least common multiple of the two positive integers entered by the user.

To sum up, the method of solving the least common multiple in C language is to use loops and conditional statements. By continuously adding a number, it is judged whether the number can be divided by two numbers at the same time until the least common multiple is found. This method is simple and effective and can be used flexibly in programming .

The above is the detailed content of How to find the least common multiple in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn