C programming

vignesh1988i's Avatar
Banned
write a C program , to find whether a number is ODD or EVEN without using Control structures , looping, arrays, functions , operators(namely relational, arithmetic , logical )??????????
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
OK, we can't use relational, arithmetic, logical operators, but we can use bit-wise and operator.

Code: CPP
if(A&1) {printf("%d is odd\n", A);}
else      {printf("%d is even\n", A);}
0
vignesh1988i's Avatar
Banned
but control structures(if... else) also should not be used...... but ur logic is right....
try that!!!!!
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
We can always replace the if...else with the conditional operator <condition> ? <true case> : <false case> .
That way, the code becomes :
Code: CPP
(A&1) ? printf("%d is odd\n", A) : printf("%d is even\n", A);
0
vignesh1988i's Avatar
Banned
hmmmm then i ll put my question , in this way... there is a way to print without this conditional operator also.... try

thank u
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Then I would do it this way

Code: CPP
char Result[2][5] = {"EVEN", "ODD"};

printf("%d is %s\n", A, Result[A&1]);
0
vignesh1988i's Avatar
Banned
hmmmm ..in my question i already said without using arrays toooo u should do it!!!!!!!
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Code:
printf("1 for odd, 0 for even -> %d \n",A&1);
If that still doesn't answer, please copy and paste the ORIGINAL question, not your summary of what you think it says.
0
vignesh1988i's Avatar
Banned
hmmmmmm this is the right answer.....
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Quote:
Originally Posted by vignesh1988i View Post
hmmmm ..in my question i already said without using arrays toooo u should do it!!!!!!!
My mistake

Quote:
Originally Posted by xpi0t0s View Post
Code:
printf("1 for odd, 0 for even -> %d \n",A&1);
Nice.