
-----------------------------------
kaotickid
Tue Mar 18, 2008 8:43 pm

Odd or Even Integer?
-----------------------------------
I'm wondering if there's a code out there to distinguish between an odd and even integer. This is a problem from the Turing .PDF (Chapter 14, Question 17), it's pretty much done but I just need to put the code that distinguishes whether an integer is even or odd. Thanks :D

var numbers : array 1 .. 10 of int

for i : 1 .. 10
    get numbers (i)
end for

for i : 1 .. 10
    %How to distinguish between odd or even.
    put numbers (i), "is an odd number."
    %How to distinguish between odd or even.
    put numbers (i), "is an even number."
end for


-----------------------------------
CodeMonkey2000
Tue Mar 18, 2008 8:47 pm

RE:Odd or Even Integer?
-----------------------------------
Use the modulus operation( the % sign). It gives the remainder of two integer. (eg 3%2 =1 since 2*2=2 and we have a remainder of 1).

-----------------------------------
A.J
Tue Mar 18, 2008 8:48 pm

Re: Odd or Even Integer?
-----------------------------------
there's a function in math called 'mod' thats also recognized in turing.
x mod y gives the remainder when x is divided by y.
so 17 mod 5 will give 2, since 17/5= 3 remainder 2.

so for distinguishing between odd and even, just take the number mod 2.
if it is 0 (anything divisible by 2 always gives 0, since there isn't any remainder), its even.
Otherwise if it is 1 (if it is not divisible by 2 then it has a remainder of 1), its odd!
so:

var x:int
put "Enter a number:"..
get x
if x mod 2=0 then
   put x," is a even number"
else
   put x," is an odd number"
end if

Hope this helped :D

A.J

EDIT: CodeMonkey2000 beat me to it. Oh darn :cry:

-----------------------------------
kaotickid
Tue Mar 18, 2008 8:52 pm

Re: Odd or Even Integer?
-----------------------------------
Haha A.J, but thanks to both of you, A.J and CodeMonkey :D
Well my questions is answered :vi:

-----------------------------------
Nyrd
Tue Mar 18, 2008 8:54 pm

Re: Odd or Even Integer?
-----------------------------------


var number : int := 5

if number mod 2 = 0 then 
 put "Even"
else 
 put "odd"
end if 
% This will display "odd"


That should do it :D 
In case you're wondering, "mod" is the modulo function. It essentially finds the remainder when you divide the number by 2 (in this case).
--Remember, if you divide a number by 2 and there is no remainder it must be even!--

-----------------------------------
Nyrd
Tue Mar 18, 2008 8:56 pm

Re: Odd or Even Integer?
-----------------------------------
Wow, I was beat to it by two people  . . .
For shame . . .

-----------------------------------
agnivohneb
Tue Mar 18, 2008 9:07 pm

Re: Odd or Even Integer?
-----------------------------------

var x:int
put "Enter a number:"..
get x
if x mod 2=0 then
   put x," is a even number"
else
   put x," is an odd number"
end if


You could also use the div function and instead of equaling 0 it equals 1.

var x:int
put "Enter a number:"..
get x
if x div 2=1 then % Note the div and the 1
   put x," is a even number"
else
   put x," is an odd number"
end if 

-----------------------------------
HeavenAgain
Tue Mar 18, 2008 9:15 pm

Re: RE:Odd or Even Integer?
-----------------------------------
(eg 3%2 =1 since 2*2=2 and we have a remainder of 1).
2*2 = 2?  :shock: 

You could also use the div function and instead of equaling 0 it equals 1.
yes you could use div to check if its odd or even, but in your example .... well, you should take another look at it
if x div 2 = 1 then
 put "even"
else
 put "odd"something is odd here

-----------------------------------
agnivohneb
Tue Mar 18, 2008 9:21 pm

RE:Odd or Even Integer?
-----------------------------------
ummmmm ... think ... think ... I give up!

-----------------------------------
richcash
Tue Mar 18, 2008 9:24 pm

Re: Odd or Even Integer?
-----------------------------------
Bitwise 'and' is a bit faster than modulus I think.
if x & 1 then
   put "even"
else
   put "odd"
end if

agnivohneb, try putting in any even number other than 2 in your code. See the problem?

-----------------------------------
Saad
Tue Mar 18, 2008 9:28 pm

Re: Odd or Even Integer?
-----------------------------------
If you want to be crazy and go with some mutual recursion ;)

forward fcn isEven (n : int) : boolean
forward fcn isOdd (n : int) : boolean

body fcn isEven
    if (n = 0) then
        result true
    else
        result isOdd (n - 1)
    end if
end isEven

body fcn isOdd
    if (n = 0) then
        result false
    else
        result isEven (n - 1)
    end if
end isOdd

put isOdd (1)
put isEven (1)


-----------------------------------
agnivohneb
Tue Mar 18, 2008 9:29 pm

RE:Odd or Even Integer?
-----------------------------------
Wait! ... Ha! I got it. I reject my last comment. I know what the problem is now. It is not accurate.

-----------------------------------
A.J
Tue Mar 18, 2008 9:42 pm

Re: Odd or Even Integer?
-----------------------------------
nice richcash :clap:
 bits are the way to go.
 
 as for you saad.......lets just say forget about mutual recursion for now, shall we :D

-----------------------------------
CodeMonkey2000
Wed Mar 19, 2008 2:56 pm

RE:Odd or Even Integer?
-----------------------------------
Wow talk about overkill.......
