Thursday 18 November 2010

test a number is even or odd using bitwise operators

//test a number is even number or odd
#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 printf ("enter any number\n");
 scanf("%d",&n);
 if(n&1)
  printf("ODD number\n");
 else
  printf("EVEN number\n");
  getch();
 }

Note:
input must e decimal number but not  binary number. system converts decimal to binary since &1  is Bit-wise operator.

output:
1. 25
odd
2. 46
even number


No comments:

Post a Comment