Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

24 Jul 2012

C Program to find Number Even or Odd using Bitwise AND ( & ) Operator

c program to understand the usage of Bitwise Operator & .
                   (Or)
Example c program for Bitwise operator & ..

PROGRAM :

#include<stdio.h>
#include<conio.h>
main()
{
    int n,i;

    printf("\n Enter a Integer Number to find Even or Odd:");
    scanf("%d",&n);
    i=!(n&1);  /* & is a bitwise and operator*/
    if ((i))
        printf("\n The Entered Number Is Even");
    else
       printf("\n The Entered Number is Odd");

    getch();
    return(0);
}


Sample Output: (using GNU GCC Compiler with Code Blocks IDE)
  1) Enter a Integer Number to find Even or Odd: 5
     The Entered Number is Odd
  2) Enter a Integer Number to find Even or Odd:
     The Entered Number Is Even
  


      n       1     (n&1)     !(n&1)
    
n=2  0010   0001    0000      returns 1 ( if statment executes)    
n=3  0011   0001    0001      returns 0 ( else statment executes)

Truth Table:

    p    q     p&q

    0    0      0
    0    1      0
    1    0      0
    1    1      1

3 comments:

  1. C program to check odd or even

    In general Even numbers are those which are divisible by 2, and which numbers are not divisible 2 is called odd numbers.
    We can easily write even odd program in c programming.

    ReplyDelete
  2. 7 easy ways to find whether a number id odd/even.
    http://www.waytosmart.com/tutorials/c-programming/c-program-to-check-odd-or-even-number-easy-ways.php

    ReplyDelete
  3. nice article .thank you.
    web programming tutorial
    welookups

    ReplyDelete