Find out Odd and even number in different ways

We can use different ways to find odd or even number.

Modulo 2 operation

It is easy to find out the even or odd number by modulus operator. Modulo 2 can find out the remainder. If the modulo  2 of the number is 0 then the number is even, otherwise it is odd.

if(n%2)
printf("odd\n");
else
printf("even\n");

From the above code, we can easily understand that when n%2 is 1, the n is odd otherwise even.  We can see that in the above code we use (n%2),  not using (n%2==1) because in the bracket of if we need to check true or false. here, 0 means flase and 1 means true. For the odd number the modulo 2 is 1 while the even number is 0. Relational equal operator is not needed here. That’s why we skip the == operator.

Bitwise and (&) operator

We can check the odd even by using bitwise & operator. In the below code, n&1 will return 1 when it is odd and otherwise it will be 0. if the number is 7 then the binary format  of the number is 111 and doing bitwise ‘and’ operator with 1 will return 1.

0111 <— binary of 7

&     <— bitwise and

0001  <— binary of 1

——

0001    <— binary format

=1 <– decimal number

If the number is 4 then the binary format of the number is 100 and by doing bitwise and operation it will return 0.

0100 <— binary of 4

&        <— bitwise and

0001   <— binary of 1

——

0000  <— binary format

=0    <– decimal number

 

if(n&1)
printf("odd\n");
else
printf("even\n");

Without using modulo (%) operator

We can find out odd even without using % modulus operator. By subtracting the number dividing by 2 and multiplying by 2 from the number, we can have one for odd number otherwise zero for even numbers.

Suppose, 7-(7/2)*2 =7-3*2=7-6=1

6-(6/2)*2=6-3*2=6-6=0

if(n-(n/2)*2)
printf("odd\n");
else
printf("even\n");

One bit left shifting does the operation of division whereas right shifting does the operation of multiplication by 2. So the above code can be rewritten  in the below code.

if(n-(n>>1)<<1)
printf("odd\n");
else
printf("even\n");

If you do not understand the shifting operation, the explanation is given in the later part.

Conditional Operator

By using conditional operator, we can replace if -else statement. We need to write less code for that.   In the conditional operator we have 3 expressions.

1st expression is the relational operator, which gives us true or false , 2nd expression is the statement if the relation is true and last part is the expression if the relation is false. After the relational operation we need to put ‘?’ sign and ‘:’ sign will be used as a separator for the true and false expression.

if(n%2==1)
printf("odd\n");
else
printf("even\n");

The above code can be rewritten by the conditional (ternary) operator.

char *output;
output=(n%2==0)?"even\n":"odd\n";
printf(output);

In the above code, *output indicates the character pointer. Character pointer means collection of character. We need the declare the character at the beginning of the code where we declare the variable.

In the conditional operator if the remainder(modulo) 2 of the number is 0 then the number is even and otherwise odd.

we can write the code using ternary operator (conditional operator). The below code will bring the same output.

We can rewrite the above code, on the following way:

printf((n%2==0)?"even\n":"odd\n");

print function will print the string (collection of characters) and we need not to use the pointer output as an extra variable.

Right shift and left shift

We can rewrite the modulo operation by shifting operator.

The modulo 2 operation can be replace by doing the right shifting and left shifting. When we do right shift that means the number is divided by 2 that means last bit of the binary number will be omitted.   for example, n=7;

n>>1 means (7>>1)

=(111>>1)  <— this is in binary form

=(011)  <— binary form

=3 <– decimal form

When we do left shift then the number is multiply by 2 that means extra 0 will be added at the end (right side).   for exam, n=7; n<<1 means 7<<1

=(111<<1) <– this is in the binary form

=(1110) <— binary form

=14 <— decimal form

if we do the both operation on the number it will not be equal if it is odd. But the for the even number it will be equal the previous number.

x=n;
n=n>>1;
n=n<<1;
printf((n==x?"even\n":"odd\n");

Suppose, n=7; x=n=7;

n=n>>1 <— now the n is 3

n=n<<1 <— now n is 6

Here, n and x are not equal. So  it is odd number.

Again, n=8;x=n=8;

n=n>>1 <— now the n is 4

n=n<<1 <— now n is 8

Here, n and x are  equal. So  it is even number.

In the above code, we use variable x  as an extra variable. We can escape using the variable and write the code like this:

n==(n>>1)<<1

The above expression will be true if the number is even otherwise odd. So we can write the expression in the printf function.

printf((n==(n>>1)<<1)?"even\n":"odd\n");

 

Big number odd or even

For the big numbers, we can handle the numbers by using string. For that, we need to declare string at first using char n[10000]. We can get the last digit of the number from the length of the string then subtract 1 from it.

l=strlen(n);
last_digit=(int) (n[l-1]-'0');

printf((last_digit==(last_digit>>1)<<1)?"even\n":"odd\n");