Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to check for odd or even?

  Asked By: Nicole    Date: Apr 04    Category: Java    Views: 1610
  

how do i check a random number generated for odd or even? would i do
the modulus?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Casey Montgomery     Answered On: Apr 04

The simple way is to just check  the number  mod 2. If you get 1 it's
odd, otherwise even. The somewhat faster way on many systems
(although it probably won't matter unless you execute this code
millions of times in a loop) would be to check the bitwise and of the
number with 1. Again, if the result is 1 the number is odd,
otherwise even.

 
Didn't find what you were looking for? Find more on how to check for odd or even? Or get search suggestion and latest updates.




Tagged: