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

Printable View

- 01-31-2002WahRe : Logic problem
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 - 01-31-2002Nutshell
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;

}

- 01-31-2002PreludeCode:
`void check(int a)`

{

if (a % 2 == 1)

printf("a is odd\n");

else

printf("a is even\n");

}

- 01-31-2002Nutshell
isn't that the same as my one lol?

- 01-31-2002Prelude
Yes, but we posted at approximately the same time.

-Prelude - 01-31-2002Shiro
>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. :-)