Dear All,
I don't understand why the following function is wrong, can anyone tell me :
void check(int a) {
if (a / 2 == 1)
printf("a is odd\n");
else
printf("a is even\n");
}
Thanks a million.
Best regards,
Wah
This is a discussion on Re : Logic problem within the C Programming forums, part of the General Programming Boards category; Dear All, I don't understand why the following function is wrong, can anyone tell me : void check(int a) { ...
Dear All,
I don't understand why the following function is wrong, can anyone tell me :
void check(int a) {
if (a / 2 == 1)
printf("a is odd\n");
else
printf("a is even\n");
}
Thanks a million.
Best regards,
Wah
If you think about it carefully, a number / 2 == 1 is not working. What if the number is 11 ? 11 / 2 = 5. I think you wanna do somethin like this:
Code:checkEven( int a ) { if ( a % 2 == 0 ) return 1; else return 0; }
-PreludeCode:void check(int a) { if (a % 2 == 1) printf("a is odd\n"); else printf("a is even\n"); }
My best code is written with the delete key.
Yes, but we posted at approximately the same time.
-Prelude
My best code is written with the delete key.
>void check(int a) {
>if (a / 2 == 1)
>printf("a is odd\n");
>else
>printf("a is even\n");
>}
A logical answer why this is wrong:
a / 2 = 1 if and only if a = 2
By the way, in your function 2 is odd. :-)