User forums > Using Code::Blocks
Compile Problem
(1/1)
dsdeiz:
Hello, I've got this really weird problem.
--- Code: ---#include <stdio.h>
int gcf(int a, int b) {
int tmp;
if (a < b) {
tmp = a;
a = b;
b = tmp;
}
if (a % b == 0) {
return b;
}
else {
gcf(b, (a % b));
}
}
int lcm(int a, int b) {
return (a / gcf(a, b)) * b;
}
int main(int argc, char *argv[]) {
int a, b, x, y;
printf("Enter integer 1: ");
scanf("%d", &a);
printf("Enter integer 2: ");
scanf("%d", &b);
x = gcf(a, b);
y = lcm(a, b);
printf("\nGreatest common factor: %d\n", x);
printf("Least common multiple: %d\n", y);
return 0;
}
--- End code ---
In Linux, doing gcc -o program program.c works just fine. But I'd also like to compile it for Windows so I tried Code Blocks. A sample input of 12 and 36 works just fine but an input of 18 and 84 gives a result of 2009155360 and 0. :? Any things I should consider? Looking forward to your replies.
Thanks!
GeO:
Shouldn't it be:
--- Code: --- else {
return gcf(b, (a % b));
}
--- End code ---
And 18 and 84 returns me 6 and 252, which should be right.
Attention, this has nothing to do with Code::Blocks, please ask this question on a c/c++ forum.
greets GeO
dsdeiz:
Oh right. My bad.. Already updated the code. Thanks! Though I still have the same problem. :(
koso:
Have you tried to debug? At least you should try some debug console output and check input values in functions, also check user input -> thats I think only place dependent on platform.
PS: that code is not very safe. Lcm function is vulnerable to "division by zero" error.
Jenna:
This problem is not C::B related.
Most likely a problem with formatting your input (or output) and might be related to your system 32- aor 64-bit.
Nevertheless your post violates our forum rules, because the C::B forums are not related to general programming questions, so the topic gets locked.
Navigation
[0] Message Index
Go to full version