gpt4 book ai didi

c - 我的 C 函数有什么问题?最大公约数和最小公倍数

转载 作者:行者123 更新时间:2023-11-30 21:27:52 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>

// Function to find the greatest common divisor
int findGCD(int numberOne, int numberTwo) {

int i, GCD;

for (i = 1; i <= numberOne && i <= numberTwo; i++) {
if (numberOne % i == 0 && numberTwo % i == 0);
}

GCD = i;

return GCD;
}

// Function to find the least common multiple using the GCD function

int findLCM(int numberOne, int numberTwo) {

int GCD, LCM;

GCD = findGCD(numberOne, numberTwo);
LCM = (numberOne * numberTwo) / GCD;

return LCM;
}

// Main function to output the lcm
void main() {

int numberOne, numberTwo, LCM;

printf("Please enter two numbers: ");
scanf_s("%i %i", &numberOne, &numberTwo);

LCM = findLCM(numberOne, numberTwo);

printf("The LCM of %i and %i is %i\n", numberOne, numberTwo, LCM);

system("pause");
}

最佳答案

你的 findGCD 不能完美地计算 GCD。您放错了 GCD = i

// Function to find the greatest common divisor
int findGCD(int numberOne, int numberTwo) {

int i, GCD;

for (i = 1; i <= numberOne && i <= numberTwo; i++) {
if (numberOne % i == 0 && numberTwo % i == 0) {
GCD = i;
}
}

return GCD;
}

关于c - 我的 C 函数有什么问题?最大公约数和最小公倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47664421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com