What is the definition of...

wicktron

Platinum Member
Aug 15, 2002
2,573
0
76
an even number and an odd number using logical operators. (e.g. || = or; && = and; == = equals).
 

Heisenberg

Lifer
Dec 21, 2001
10,621
1
0
You usually only deal with binary, and so just have 0 and 1. I'm kind of confused on what you're asking though.
 

wicktron

Platinum Member
Aug 15, 2002
2,573
0
76
Well, I'm trying to get this program to tell the difference between even and odd numbers... (e.g. the integer 4; even? true; odd? false). Things of that nature.
 

PowerMacG5

Diamond Member
Apr 14, 2002
7,701
0
0
say x is the number.
(this is pseudocode)

if( x mod 2 equals 0)
then the number is even
else
the number is odd
 

wicktron

Platinum Member
Aug 15, 2002
2,573
0
76
Sorry, should have specified that I can't use conditional statements (e.g. if, then, else)
 

Heisenberg

Lifer
Dec 21, 2001
10,621
1
0
You'd probably have to use the 'mod' function and use 2. If the number is even, the value will be 0. If it's odd, it will be nonzero. You can then use that for your logic statements.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
can you use bitwise operators like &

because if a number is even it's lowest bit in binary will be 0, else 1 so you can "and" the number with 1 to check the loest bit

i don't think logical operators can do it
 

wicktron

Platinum Member
Aug 15, 2002
2,573
0
76
I can use &&, !, ==, ||.
The language is Java, say I have an int i, i being an integer, I must give characteristics of it such that I get even? odd? positive? zero? negative?
The last 3 are obvious to me, but the others I can't seem to get in code. In English, fine, but using operators, I'm lost.

For example, if I'm giving the characteristic for a negative number, then: i < 0; for positive: i > 0; for zero: i == 0
Then prompting a user to input an integer, 7, for example, then:

even? false; odd? true; positive? true; zero? false; negative? false