Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have just started my C++ lecture class. And the teacher has given us the following assignment.

Write a program that determines whether a number is even or odd.

The logic I would use for the program is.

  1. Get input a.
  2. Store a % 2 as b.
  3. If b is 0, then a is even, else a is odd.

The catch though, is that we have to write the program without the use of a if-else statement.

I have been thinking on how to approach the problem for the past few hours, but I have no clue what to do. Any hints or suggestions?

share|improve this question

closed as too localized by DeadMG, Mark Trapp, gnat, ChrisF Jun 3 '12 at 19:52

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Maybe the ? operator is your friend? –  Giorgio Jun 2 '12 at 17:00
@Giorgio I'd consider that an if/else. –  Andrew Finnell Jun 2 '12 at 17:00
@Giorgio - that's still an if/else –  ChrisF Jun 2 '12 at 17:01
This is a silly problem with silly solutions that's way too localized to help anyone else. –  DeadMG Jun 2 '12 at 17:48
@ChrisF Belongs on SO? –  Dynamic Jun 2 '12 at 20:06
show 11 more comments

2 Answers 2

Here's one way to do it:

int a;
/* get input and place into a... */
int b = a % 2;
const char *values[] = {"even", "odd"};
const char *result = values[b];
/* output result */

Note that the ternary-operator (?:) is not using if-else in the syntactic sense, although it would be compiled down to the same machine instructions.

Ditto, you could use the condition of a while loop to check what you need. Simply break out of it, and that too should be optimized down to the same machine instructions. The same would go for using a switch, which might've been what your instructor was trying to get at.

If you want to make better use of data structures, you could use a map structure (also known as dictionaries and associative arrays) to give you a generalization against many cases. The values stored in the map could even be function pointers or functors (in your simpler case, a string would be sufficient as the value).

share|improve this answer
Is char *values[] = {"even", "odd"}; legal? I thought converting string literals to char* was deprecated. –  Pubby Jun 2 '12 at 19:17
@Pubby, of course it's legal. I don't think they deprecated char *argv[]. =P –  Bryan Dunsmore Jun 2 '12 at 19:29
This is exactly what I would do. However, I think Pubby's point is that the char pointers should have been const char pointers. –  user16764 Jun 2 '12 at 19:49
@dunsmoreb char *argv[] as an argument is equivalent to char **argv and there's no string literal involved so it's irrelevant. user16764 is correct in that const is correct here. –  Pubby Jun 2 '12 at 20:18
Thanks! I added in the const. My only defense is that it was pseudo code! :-) You don't want to do all of the HW assignment. –  Macneil Jun 3 '12 at 3:19
add comment

a % 2 already tells you whether it's even or odd.

std::cout << std::boolalpha << a % 2; // outputs true or false
share|improve this answer
No. It tells you whether it is true or false. –  Bryan Dunsmore Jun 2 '12 at 17:54
@dunsmoreb: Which is exactly the same thing. "Is a odd?" "true/false". –  DeadMG Jun 2 '12 at 17:56
The output would be more like: 5 false. –  Bryan Dunsmore Jun 2 '12 at 18:04
@dunsmoreb: Why would it be 5 false and not just true? –  DeadMG Jun 2 '12 at 22:06
For one, five is odd. =P And two, I'm assuming the input would be echoed. –  Bryan Dunsmore Jun 2 '12 at 22:07
show 2 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.