Search Tutorials

Wednesday, 17 April 2013

C code to check number is even or odd without any comparison

This is very simple to check a number is even or odd because we can get answer to check only last bit of a number. Simple AND with 1 and  if answer is zero means number is even else odd.

C program:

#include<stdio.h>

int main()
{
int number,check;
number=10;

//take any number as an input
check=number&1;
//this will check first bit ,if bit is zero means even otherwise odd
if(check)
printf(" Number is Odd\n");
else
printf(" Number is Even\n");
getch();
return 0;
}

Output of the above program:

Check-even-and-odd-number
Check even and odd number 

No comments:

Post a Comment

Back to Top