C Programming Reference >> C Programming Reference Question Bank >>
19/07/07 - Views - Ratings : 0 of 5 / Votes
Level - Beginner
Question -
Write a program to find and print the given number is odd or even, Using only one printf (output) statement, no conditional Statement and no logical,relational and arithmatic operators.
Answer -
#include <stdio.h> main() { int x;
scanf ("%d",&x); printf ("%d",x&1); }
Explanation -
This an easy to understand code. What we have done here is use the bitwise AND operator and use it to bitwise AND the being evaluated number with 1.
The code will print 1 when number is odd and 0 when number is even, which do task in the question quite efficiently.
Reader Comments -
| girish tiwari
|
good question bit vise operators are very wise!!
|
| prince
|
its very superb idea.
|
| Shoukat Ali
|
Good to know how Logical operators efficiency
|
| Santosh Tripathy
|
Excellent use of logic
|
| Yerra Somesh
|
main() { char nm["Even","Odd"]; int x; scanf("%d",&x); printf("%s",nm[x%2]); }
|
| r a simnani
|
nice explained
|
| r a simnani
|
nicely explained
|
| akhil kumar
|
@yerra somesh....we are not supposed to use arithmetic operators... but ypur logic semms entirely different so plzzz if u cn explain it..
|
| Chethan C
|
nice question.....
|
| siddhartha saxena
|
nice good logic
|
| Rahul singh
|
@Akhil here num is an char array whose index value is 0,1 so when % came it prints the value of 0,1 of array....
|
| sachindra
|
good thinking.very well logic
|
| alok kumar jha
|
good logic
|
| Gopi
|
Why use bitwise AND? Operators should not be used.
|
| augustino
|
idon`t understanding this solution and questio n
|
| kartik
|
I impressed..............!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|