Code.GeekInterview.com
 
Code Samples C
 

Finding a number is even or odd without using arithmatic operators


Code ResourceAuthor: sadasivathavamani  

Difficulty Level: Intermediate

Published: 9th Nov 2010   Read: 15046 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

logical operators plays a bit2bit operations.&operators chechs whether the bit is present at perticular position or not. if 1 is at lsb(right most in binary) then that no is odd.

 


Sample Code
  1. #include<stdio.h>
  2. main()
  3. {
  4.     int i,j;   /*int type variables*/
  5.     scanf("%d",i);/*read i value*/
  6.     j=i&1;    /*perform logical and operation to find 1at lsb
  7.     if(j==0)
  8.         printf("even");
  9.     else
  10.         printf("odd");
  11.      getch();
  12. }
Copyright GeekInterview.com


Next Article: Brute force solver for Challenger number puzzle.


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

program not run because logic is wrong
Comment posted by: vinod thete on 2011-02-04T04:26:40

Popular Coders

# Coder NameHits
1. kaivalya198948877
2. mano.mithun39826
3. deepu0821803
4. sadasivathavamani15047
5. venkatakrishnansvpr13075
6. Jimmy Zorald12847
7. meefriend4ever10577
8. chandrikakr10391
9. shashivaishnav10362
10. sripri8634

Active Coders

Refined Tags