Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Odd or Even Integer?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kaotickid




PostPosted: Tue Mar 18, 2008 8:43 pm   Post subject: 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 Very Happy

code:
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
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Tue Mar 18, 2008 8:47 pm   Post subject: 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




PostPosted: Tue Mar 18, 2008 8:48 pm   Post subject: 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:
Turing:

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 Very Happy

A.J

EDIT: CodeMonkey2000 beat me to it. Oh darn Crying or Very sad
kaotickid




PostPosted: Tue Mar 18, 2008 8:52 pm   Post subject: Re: Odd or Even Integer?

Haha A.J, but thanks to both of you, A.J and CodeMonkey Very Happy
Well my questions is answered BooHoo
Nyrd




PostPosted: Tue Mar 18, 2008 8:54 pm   Post subject: Re: Odd or Even Integer?

code:


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 Very Happy
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




PostPosted: Tue Mar 18, 2008 8:56 pm   Post subject: Re: Odd or Even Integer?

Wow, I was beat to it by two people . . .
For shame . . .
agnivohneb




PostPosted: Tue Mar 18, 2008 9:07 pm   Post subject: Re: Odd or Even Integer?

A.J @ Tue Mar 18, 2008 8:48 pm wrote:
Turing:

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.

Turing:
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




PostPosted: Tue Mar 18, 2008 9:15 pm   Post subject: Re: RE:Odd or Even Integer?

CodeMonkey2000 @ Tue Mar 18, 2008 9:47 pm wrote:
(eg 3%2 =1 since 2*2=2 and we have a remainder of 1).

2*2 = 2? Shocked

agnivohneb @ Tue Mar 18, 2008 10:07 pm wrote:
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
code:
if x div 2 = 1 then
 put "even"
else
 put "odd"
something is odd here
Sponsor
Sponsor
Sponsor
sponsor
agnivohneb




PostPosted: Tue Mar 18, 2008 9:21 pm   Post subject: RE:Odd or Even Integer?

ummmmm ... think ... think ... I give up!
richcash




PostPosted: Tue Mar 18, 2008 9:24 pm   Post subject: Re: Odd or Even Integer?

Bitwise 'and' is a bit faster than modulus I think.
code:
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




PostPosted: Tue Mar 18, 2008 9:28 pm   Post subject: Re: Odd or Even Integer?

If you want to be crazy and go with some mutual recursion Wink

Turing:
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




PostPosted: Tue Mar 18, 2008 9:29 pm   Post subject: 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




PostPosted: Tue Mar 18, 2008 9:42 pm   Post subject: Re: Odd or Even Integer?

nice richcash Claping
bits are the way to go.

as for you saad.......lets just say forget about mutual recursion for now, shall we Very Happy
CodeMonkey2000




PostPosted: Wed Mar 19, 2008 2:56 pm   Post subject: RE:Odd or Even Integer?

Wow talk about overkill.......
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: