How do I check whether the input is even number or whether it's an odd number?

When I say the input I meanSomething like that.Code:`char str[] = "12345";`

scanf("%s", str);

Printable View

- 11-22-2007eXeCuTeREven/Odd number.
How do I check whether the input is even number or whether it's an odd number?

When I say the input I meanCode:`char str[] = "12345";`

scanf("%s", str);

- 11-22-2007laserlight
You can either read it in as an integer, then check if the integer is even/odd, or you can read it in as a string then check if the last character is even/odd.

For actually determining if a number is even/odd, there are a number of ways. The straightforward way is to check if n % 2 is 0, the other involves bitwise and, which is also straightforward if you think in terms of binary representation. - 11-22-2007eXeCuTeR
I thought of the num % 2 thing.

Look what I tried:

Code:`char str[] = "12345";`

printf("Enter a number:\n");

ch = getchar();

if(ch%2 == 0) oddNumber(ch);

if(ch%2!=0) evenNumber(ch);

Code:`ch = scanf("%s", str);`

- 11-22-2007laserlightQuote:

Will it work with getchar?

- 11-22-2007eXeCuTeR
Hmm, I haven't actually compiled and run it, lol :D

I just typed it on the same moment lol :) - 11-22-2007laserlightQuote:

Hmm, I haven't actually compiled and run it, lol