C program to check a number is even or odd without modulus or division condition.

To check an integer number is even or odd by using bitwise operator in C programming language:




An integer number is even if it is completely divisible by the 2 i.e. the modulus by 2 is zero. So it is necessary to dividing a number or determines the modulus 2 of a number to decide whether a number is even or not.

One another procedure to identify whether a integer number is even or odd by using ‘BIT WISE’ operator. The bit wise AND ‘&’ operator is suitable to perform the operation to check whether a number is even or odd.

Before starting C program to check a integer number is even or odd with Bit Wise AND ‘&’ operator. Let’s see how Bit Wise AND ‘&’ operator will behaves.


Bitwise AND Operator:


The Bitwise AND will take pair of bits from each position, and its output will be 1 if and only if both the bit is 1. Bitwise AND is used to Turn-Off bits. Bit wise operators will work on bits at a time.

InputOutput
AB
000
010
100
111

Truth table of Bitwise AND operator
For example:




  1. 1010
    1100
    ——-
    AND    1000
    ——-

Take two numbers 2 and 1

  1. 010 :2
    001  :1
    —–
    000  = 0 (Number is even)
    —–
  2. 100 :4
    001  :1
    —–
    000  = 0 (Number is even)
    —–
  3. 101 :5
    001  :1
    —–
    001  ≠ 0 (Number is odd)
    —–
  4. 111 :7
    001  :1
    —–
    001  ≠ 0 (Number is odd)
    —–
    By analysis of the above examples we can say that on every even number Bit Wise AND ‘&’ with 1 gives 0 always.

So this Bit Wise AND operational logic to be implemented in our program i.e. if “A number & 1= = 0” then the number is even number, else number is odd.




Program 1

#include <stdio.h>
#include <conio.h>
void main()
{
int a;
printf(“Enter a number”);
scanf(“%d”,&a);
if((a&1)= =0)
{
printf(“Number is even”);
}
else
{
printf(“number is odd”);
}
return 0;
getch();
}
Output:
Enter a number 5
Number is odd.
——————
Enter a number 98
Number is even.




To check an integer number is even or odd by using Shift Operator in C programming language:




We can check an integer number is even or odd with one another operator which is shift operators. Although it may be not a better solution but trying to cover alternative to check a number is even or odd without divide or modulus operator.

Let’s see how shift operator will behaves.
Lets do this 5 >> 1

5 means 101 so now we need to shift right side by 1 place.

101 >> 1 = 010

Similarly; do this 2 >> 1


2 means 010 so now we need to shift right side by 1 place

010 >> 1 = 001

Here we added one “0” to the left side to shift right. So the value became 1.
Now we will try left shift 001 << 1

001 << 1 = 010 010 >> 1= 001 (1) and 001 << 001= 010 (2)




Here we notice that if we shift one time right and again shift one time left and the number is unchanged then it will be an even number although if number is changed i.e. after shifting both direction (first right shift then left shift) the updated number is not same as the input number then the number is odd.

Some more examples;

7 >> 1 = 111 >> 001 = 011 << 001 = 110
(Input number is 111 and after shifting number is 110 hence number is odd)

8 >> 1 = 1000 >> 001 = 0100 << 001 = 1000
(Input number is 1000 and after shifting number is 1000 hence number is even)

11 >> 1 = 1011 >> 001 = 0101 << 001 = 1010
(Input number is 1011 and after shifting number is 1010 hence number is odd)

6 >> 1 = 110 >> 001 = 011 << 001 = 110
(Input number is 110 and after shifting number is 110 hence number is even)

Program 2

#include <stdio.h>
#include <conio.h>
int main()
{
int a;
printf(“Enter a number”);
scanf(“%d”,&a);
if((a>>1)<<1==a)
{
printf(“Number is even”);
}
else
{
printf(“number is odd”);
}
return 0;
getch();
}
Output:
Enter a number 15
Number is odd.
————————–
Enter a number 98
Number is even.




LEAVE A REPLY

Please enter your comment!
Please enter your name here