views:

858

answers:

7

Hi,

I was recently asked to write a program, that determines whether a number is even or odd without using any mathematical/bitwise operator!

Any ideas?

Thanks!

+3  A: 

You could convert the number into a string and check if the last digit is 1,3,5,7 or 9.

pablochan
The string conversion routine probably uses the (mathematical) modulus operator internally to extract the number's digits.
Amnon
yes that can be done! But it a whole lot inefficient to convert that to string and then check
@Amnon: the single-bit fields proposed elsewhere use bitwise operators internally. It's just a matter of how stupid you think this interview puzzle is: "very" or "extremely".
Steve Jessop
+2  A: 

I'm not sure if == is considered as a math operator, if no, then convert the number to a string and test if the last char is equal to 0, 2, 4, 6 or 8.

Edit: == seems to be considered as a Comparison operator/Relational operator.

Soufiane Hassou
+3  A: 
switch (n) {
    case 0:
    case 2:
    case 4:
    ...
        return Even;
}
return Odd;
UncleBens
ha, look what we never thought of!
Here Be Wolves
Why is this downvoted?
Lex
Why the downvotes? Without relational and bitwise operators i don't see anything better. (+1)
Georg Fritzsche
downvotes => when will this thing end ? 0,2,4,6,8,10,12,... ?
Soufiane Hassou
You could use non-portable casts to get a char* to the least significant byte of the int and then your case statement would be bounded to only 0...254
John Burton
+25  A: 

This can be done using a 1 bit field like in the code below:

#include<iostream>

struct OddEven{
    unsigned a : 1;
};
int main(){
    int num;
    std::cout<<"Enter the number: ";
    std::cin>>num;
    OddEven obj;
    obj.a = num;
    if (obj.a==0)
        cout<<"Even!";
    else
        cout<<"Odd!";
    return 0;
}

Since that obj.a is a one-field value, only LSB will be held there! And you can check that for your answer.. 0 -> Even otherwise Odd..!!

Srivatsan Iyer
non-portable, but creepily artistic
Eli Bendersky
If you make the bitfield `unsigned` instead of `int`, it becomes perfectly portable!
caf
yes! Thanks for pointing, I did the edition..!
Srivatsan Iyer
"unsigned a : 1", hmm, never seen that syntax.
Viktor Sehr
__FAIL__: If the number is negative bit 1 may be 0 for odd numbers if the underlying integer representation is 1's compliment.
Martin York
Martin York, actually, this will work regardless of representation: "If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer"
avakar
+1  A: 

... Why would you do this?

This is only possible if you're trying to avoid writing +, -, /, *, &, | ^, or %.

Switches and string conversion have implicit lookup tables, and thus implicit additions.

The following looks to avoid it:

//C/C++ code (psuedo'd up)
struct BitNum
{
  unsigned int num : 1;
};

...
BitNum a;
a.num = number_to_be_tested;
if(a.num == 0) return EVEN;

return ODD;
...

But it implicit uses & to get to just the bit.

Kevin Montrose
+1  A: 

This is a weird challenge.

You could use the number as a memory address offset for a load instruction. If your architecture requires memory access to be aligned on two-byte offsets, then the processor will allow loads from even addresses and throw an exception for an odd address (unaligned load).

benzado
What if there are some even addresses that it throws an exception for (for instance, values outside the range of its memory map)?
Steve Jessop
That is left as an exercise to the reader.
benzado
+11  A: 

Most concise solution to your problem :

#include <iostream>
#include <string>
#include <bitset>

int main() {
  std::string const type[] = {"even", "odd"};
  int n;
  std::cout << "Enter an integer." << std::endl;
  std::cin >> n;
  std::cout << type[(std::bitset<1>(std::abs(n))[0])] << std::endl;
}
missingfaktor