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
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);
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.
I thought of the num % 2 thing.
Look what I tried:
Will it work with getchar? or should I:Code:char str[] = "12345";
printf("Enter a number:\n");
ch = getchar();
if(ch%2 == 0) oddNumber(ch);
if(ch%2!=0) evenNumber(ch);
Is that even possible?Code:ch = scanf("%s", str);
You say you tried. What did you find out? ;)Quote:
Will it work with getchar?
Hmm, I haven't actually compiled and run it, lol :D
I just typed it on the same moment lol :)
Then compile what you have and test if it works to your expectations.Quote:
Hmm, I haven't actually compiled and run it, lol