| Author | 
		Message | 
	
		 
		kaotickid
 
 
 
    
		 | 
		
		
			
				  Posted: 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  
 
 
	  | 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 
		 
  
		 | 
		
 | 
	
	 
		  | 
	
				 
		CodeMonkey2000
 
 
 
    
		 | 
		
		
			
				  Posted: 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
 
  
 
    
		 | 
		
		 | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		kaotickid
 
 
 
    
		 | 
		
		
			
				  Posted: 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  
 
Well my questions is answered   | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		Nyrd
 
  
 
    
		 | 
		
		
			
				  Posted: 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   
 
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
 
  
 
    
		 | 
		
		
			
				  Posted: 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
 
  
 
    
		 | 
		
		
			
				  Posted: 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
 
  
 
    
		 | 
		
		
			
				  Posted: 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?    
 
 
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 
		 
  
		 | 
		
 | 
	
	 
		  | 
	
				 
		agnivohneb
 
  
 
    
		 | 
		
		
			
				  Posted: Tue Mar 18, 2008 9:21 pm    Post subject: RE:Odd or Even Integer?  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| ummmmm ... think ... think ... I give up! | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		richcash
 
 
 
    
		 | 
		
		
			
				  Posted: 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
 
  
 
    
		 | 
		
		
			
				  Posted: 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  
 
 
	  | 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
 
  
 
    
		 | 
		
		
			
				  Posted: 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
 
  
 
    
		 | 
		
		
			
				  Posted: Tue Mar 18, 2008 9:42 pm    Post subject: Re: Odd or Even Integer?  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				nice richcash  
 
 bits are the way to go.
 
 
 
 as for you saad.......lets just say forget about mutual recursion for now, shall we   | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		CodeMonkey2000
 
 
 
    
		 | 
		
		
			
				  Posted: Wed Mar 19, 2008 2:56 pm    Post subject: RE:Odd or Even Integer?  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| Wow talk about overkill....... | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		 |