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);
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?Will it work with getchar?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hmm, I haven't actually compiled and run it, lol
I just typed it on the same moment lol
Then compile what you have and test if it works to your expectations.Hmm, I haven't actually compiled and run it, lol
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)