Sunday, August 28, 2011

Finding a number is even or odd without using arithmatic operators

#include < stdio.h >
main()
{
     int i,j;   /*int type variables*/
     scanf("%d",&i);/*read i value*/
     j=i&1;    /*perform logical AND operation */
     if(j==0)
          printf("even");
     else
          printf("odd");

}

Math is Fun Games

1 comment:

  1. its simple & is here a logical operator also known as bitwise operator its doing bit by bit logical AND operation suppose for example you hav to find is 3 odd or even then the program works like that 3's binary form is 011 AND 001(for 1) now remember bit by bit i.e. 0 by 0 then 1 by 0 and again 1 by 1 then the result that will be stored in 'j' will be as 001 i.e 1 and is not zero so if condition fails here and else condt will be printed on the output screen i.e odd now try it for number 4 its binary equivalent is 100 AND 001(for 1) so 1 by 0 then 0by 0 and also 0 by 1 it willl be 0 and if condt is true so even printed out.......

    ReplyDelete