ADVERTISEMENT
ADVERTISEMENT

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.
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.