by nandha on April 3rd, 2007

nandha

Question

Help answer this question below.

What is the "c" code that can find, whether the given no is odd or even?. U should not use mod operator and any conditional statements?

  • Like
  • Report

Answers. 2 helpful answers below.

  • by rodschmidt on October 17th, 2007

    rodschmidt

    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];

    No comments. Post one | Permalink

  • by SEstudent on May 3rd, 2007

    SEstudent

    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;

    No comments. Post one | Permalink

Want to attach an image to your answer? Click here.

Did this answer your question? If not, then ask a new question or create a poll.

You're reading What is the "c" code that can find, whether the given no is odd or even?. U should not use mod operator and any conditional statements?

Follow us on Facebook!

Related Ads

ANSWERBAG BUZZ

Wap to print to whether the no is even or odd without using mod operator
A program to find a number is even or odd without using modulo operator
A c program to find the given number is even or odd without using any condition statements and loops
C program to find a number is odd or evrn without using the operator
C programs for odd or even without operators