Home » C solved programs » C puzzles Programs

Program to check number is whether EVEN or ODD without using any arithmetic or relational operators

We can check whether an integer number is EVEN or ODD without using any Arithmetic or Relational operators.

Here is a simple trick that we can use to check whether number is EVEN or ODD. Using Logical AND (&) operator we can check it, each EVEN number has 0th bit LOW (0) and ODD number has 0th bit HIGH (1).

The statement (number & 0x01) will check that 0th bit is HIGH, if it is HIGH (1) number will be an ODD otherwise number will be an EVEN.

#include <stdio.h>
 
int main()
{
    int number;
     
    //input an integer number
    printf("Please input an integer number: ");
    scanf("%d",&number);
         
    //check 0th bit of number is 1 or 0
    (number & 0x01) ? printf("%d is an ODD Number.", number) :  printf("%d is an EVEN Number.",number) ;
     
    printf("\n");
    return 0;   
}
    First Run:
    Please input an integer number: 100
    100 is an EVEN Number.
    
    Second Run:
    Please input an integer number: 101
    101 is an ODD Number.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.