FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   even or odd
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: even or odd  (Read 629 times)
pokemon#1

WWW Email
even or odd
« on: Jul 4th, 2003, 5:40am »

I need help using proce55ing to detect if an increasing integer is even or odd. Can anyone help?
 
skloopy

WWW
Re: even or odd
« Reply #1 on: Jul 4th, 2003, 7:33am »

yup. just use the modulo operator (%).
 
if(n % 2 == 0) then n is even.
 
Modulo wraps the numder around itself, so it keeps looping. I can't explain it very well, so look on tha java tutorial here :^P
 
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arithmetic.htm l
 
benelek

35160983516098 WWW Email
Re: even or odd
« Reply #2 on: Jul 4th, 2003, 3:29pm »

you've already got an answer there, but here's a cute little piece of maths i learnt during my days in highschool:
 
(-1)^num
 
if num is even, 1 will result, and if num is odd, -1 will result. so you can use it as a coefficient where a number needs to be greater than zero if it is even, and smaller than zero if odd.
 
v3ga

WWW Email
Re: even or odd
« Reply #3 on: Jul 4th, 2003, 6:04pm »

This one should work too :  
 (n & 0x01) == true if n is odd.
« Last Edit: Jul 4th, 2003, 6:07pm by v3ga »  

http://v3ga.net
pokemon#1

WWW Email
Re: even or odd
« Reply #4 on: Jul 4th, 2003, 9:37pm »

Thanks so much. I will post my applet here soon when it is done. All of your ways work, even though I used scloopy's way because it was the first posted.
 
Pages: 1 

« Previous topic | Next topic »