ANSWERS: 2
  • You can binary AND the number with 1. If the result is zero, the original number was even, if it is one the original number was odd. int x = <somenumber>; return x & 1;
  • Actually a more realistic prohibition would be: do not use any C-specific operators. That way your program will be more portable into other languages. = = = = Solution 1: int x=<somenumber>; int halfX=x/2; int truncX=halfX*2; int isEven=(x==truncX); return isEven; = = = = Solution 2: int x=<somenumber>; int isEven[maxint-minint+1] int k=0; // note that minint is not even for(int i=minint;i<maxint;i++){ isEven[i]=k; k=1-k; } isEven[maxint]=1; // we did not want to try to make i exceed maxint return isEven[x];

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy