Is Number Odd?


Closed Thread
Results 1 to 30 of 30

Thread: Is Number Odd?

  1. #1
    T.Jackson's Avatar
    T.Jackson Guest

    Post Is Number Odd?

    Is there more than one way to test to see if an integer is even or odd? So far I've only been abe to devise one solution. The solution makes use of the AND bitwise operator.

    Code:
    public boolean isNumOdd(int num)
    {
       if (1 & num)
          return true;
       else
          return false;
    }
    PBP code ...

    Code:
    main:
       if 1 & num then
          oddNum = true
       else
          oddNum = false
       end if
    Has anyone got another way of doing this? I'm incredibly keen to hear your thoughts.

    Best Regards,

    Trent Jackson

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Code:
    	If num.0=1 then
    		oddNum = true
    		else
    		oddNum = false
    		endif

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    GrandPa's way!


    Code:
    OddNum = True
    IF Num // 2 = 0 THEN OddNum = False

    Does Num = 1 work?
    ----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    Code:
    OddNum = True
    IF Num // 2 = 0 THEN OddNum = False
    Do me a favour and refresh my memory with what "//" does?

    Trent Jackson

  5. #5
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Page 32 of the PBP manual defines it as Remainder (Modulus). I believe Modulus or Modulo is the more generally used terminology. It's also more useful, generally, than the other methods suggested as you can use it to test whether any number is evenly divisible by any other number. See...
    Last edited by dhouston; - 11th June 2008 at 13:51.

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dhouston View Post
    Page 32 of the PBP manual defines it as Remainder (Modulus). I believe Modulus is the more generally used terminology. It's also more useful, generally, as you can use it to test whether any number is evenly divisible by any other number.
    Quote Originally Posted by T.Jackson View Post
    Do me a favour and refresh my memory with what "//" does?

    Trent Jackson


    I knew it did something for sure; (well, at least at where I am it does).

    --------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89


    Did you find this post helpful? Yes | No

    Default

    in VB

    Code:
    If ((Text1.Text Mod 2) = 0) Then
            MsgBox "Even"
        Else
            MsgBox "Odd"
        End If

    .

  8. #8
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by precision View Post
    in VB

    Code:
    If ((Text1.Text Mod 2) = 0) Then
            MsgBox "Even"
        Else
            MsgBox "Odd"
        End If
    That's pretty uncanny bud, because I just wrote this ...

    Code:
    Dim num As Integer
    Dim oddNum As Boolean
    
    num = 5
    
    If num Mod 2 = 0 Then
       oddNum = False
    Else
       oddNum = True
    End If
    I reckon that there's a recursive way of doing it too!

    Trent Jackson

  9. #9
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89


    Did you find this post helpful? Yes | No

    Default

    I think only mod formula is easy way to find oddeven.

    oddeven = number mod 2
    if oddeven = 0 then page is even, otherwise odd

    .

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Talking

    How about the most rediculous, long winded, round-about way to do it?

  11. #11
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    So to sum it up so far we've got the following, verified to be working solutions.

    1. AND Bitwise operator (My initial approach)
    2. Direct bit checking (Melanie's approach)
    3. Modulus remainder operator (Precision & Sayzer's approach)

    The goal is 10 solutions -- can we do it?

    Trent Jackson
    Last edited by T.Jackson; - 11th June 2008 at 14:17.

  12. #12
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    How about the most rediculous, long winded, round-about way to do it?
    Any solution that can be verified workable!

    Trent Jackson

  13. #13
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default Double subtraction

    Code:
    testnumber var word
    main:
    if testnumber = 0 then
        lcdout "Undefined"
        stop
    endif
    testnumber = testnumber - 1
    testnumber = testnumber - 1
    if testnumber = 0 then
         lcdout "EVEN"
         stop
    endif
    if testnumber = $ffff then
         lcdout "ODD"
         stop
    endif
    if testnumber > 0 and testnumber < $ffff then lcdout "Not done yet"
    goto main
    end
    Should work, haven't tried it...but it's surely a waste of resources!

  14. #14
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Code:
    testnumber var word
    main:
    if testnumber = 0 then
        lcdout "Undefined"
        stop
    endif
    testnumber = testnumber - 1
    testnumber = testnumber - 1
    if testnumber = 0 then
         lcdout "EVEN"
         stop
    endif
    if testnumber = $ffff then
         lcdout "ODD"
         stop
    endif
    if testnumber > 0 and testnumber < $ffff then lcdout "Not done yet"
    goto main
    end
    Should work, haven't tried it...but it's surely a waste of resources!
    I agree that, that's workable. Continuously subtracting 2 from the target number until you either reach zero, which denotes an even num or -1 equating to an odd num.

    Trent Jackson

  15. #15
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    I also have another one.

    Mine does two calculations and compares the results in case the processor is going weird.
    And even more, has a bug report feature.

    Ski, is this ridiculous enough? I can go deeper.


    Code:
    </i></font>TestNumber  <font color="#000080"><b>VAR WORD
    </b></font>Index       <font color="#000080"><b>VAR BYTE
    </b></font>Result1     <font color="#000080"><b>VAR BIT
    </b></font>Result2     <font color="#000080"><b>VAR BIT
    </b></font>FinalResult <font color="#000080"><b>VAR BIT
    </b></font>Even        <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>0
    </b></font>Odd         <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>1
    </b></font>Err         <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>2
    
    </b></font>Begin:
      
      Result1 = Even
      <font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000"><b>1 </b></font><font color="#000080"><b>TO </b></font><font color="#FF0000"><b>9 </b></font><font color="#000080"><b>STEP </b></font><font color="#FF0000"><b>2
          </b></font><font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= Index <font color="#000080"><b>THEN 
             </b></font>Result1 = Odd
             Index = <font color="#FF0000"><b>11 </b></font><font color="#000080"><i>' Exit loop.
          </i><b>ENDIF
      NEXT </b></font>Index
      
    
    <font color="#000080"><i>' To make sure, do another calculation !!
    
      </i></font>Result2 = Odd
      <font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>2 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>4 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>6 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>8 </b></font><font color="#000080"><b>THEN </b></font>Result2 = Even
      
      
    <font color="#000080"><i>' And finally, compare the results;
    
      </i><b>IF </b></font>Result1 = Result2 <font color="#000080"><b>AND </b></font>Result1 = Odd <font color="#000080"><b>THEN 
         </b></font>FinalResult = Odd
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>,  <font color="#008000"><b>&quot;Result1   :&quot;</b></font>,#Result1 
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Result2   :&quot;</b></font>,#Result2
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$94</b></font>,<font color="#008000"><b>&quot;FinalResult:&quot;</b></font>,#FinalResult
      <font color="#000080"><b>ELSE
         </b></font>FinalResult = Err
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>,  <font color="#008000"><b>&quot;Unknown Error Occured!&quot;
         </b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Reporting to support page...&quot;
         </b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;weblink:www.picbasic.co.uk/forum&quot;</b></font>]
         <font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>100
         </b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;@Msg:alert@bug found&quot;</b></font>]
      <font color="#000080"><b>ENDIF    
    
    
    
    GOTO </b></font>Begin
    Last edited by sayzer; - 11th June 2008 at 16:05.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  16. #16
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    I also have another one.

    Mine does two calculations and compares the results in case the processor is going weird.
    And even more, has a bug report feature.

    Ski, is this ridiculous enough? I can go deeper.


    Code:
    </i></font>TestNumber  <font color="#000080"><b>VAR WORD
    </b></font>Index       <font color="#000080"><b>VAR BYTE
    </b></font>Result1     <font color="#000080"><b>VAR BIT
    </b></font>Result2     <font color="#000080"><b>VAR BIT
    </b></font>FinalResult <font color="#000080"><b>VAR BIT
    </b></font>Even        <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>0
    </b></font>Odd         <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>1
    </b></font>Err         <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>2
    
    </b></font>Begin:
      
      Result1 = Even
      <font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000"><b>1 </b></font><font color="#000080"><b>TO </b></font><font color="#FF0000"><b>9 </b></font><font color="#000080"><b>STEP </b></font><font color="#FF0000"><b>2
          </b></font><font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= Index <font color="#000080"><b>THEN 
             </b></font>Result1 = Odd
             Index = <font color="#FF0000"><b>11 </b></font><font color="#000080"><i>' Exit loop.
          </i><b>ENDIF
      NEXT </b></font>Index
      
    
    <font color="#000080"><i>' To make sure, do another calculation !!
    
      </i></font>Result2 = Odd
      <font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>2 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>4 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>6 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>8 </b></font><font color="#000080"><b>THEN </b></font>Result2 = Even
      
      
    <font color="#000080"><i>' And finally, compare the results;
    
      </i><b>IF </b></font>Result1 = Result2 <font color="#000080"><b>AND </b></font>Result1 = Odd <font color="#000080"><b>THEN 
         </b></font>FinalResult = Odd
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>,  <font color="#008000"><b>&quot;Result1   :&quot;</b></font>,#Result1 
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Result2   :&quot;</b></font>,#Result2
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$94</b></font>,<font color="#008000"><b>&quot;FinalResult:&quot;</b></font>,#FinalResult
      <font color="#000080"><b>ELSE
         </b></font>FinalResult = Err
         <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>,  <font color="#008000"><b>&quot;Unknown Error Occured!&quot;
         </b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Reporting to support page...&quot;
         </b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;weblink:www.picbasic.co.uk/forum&quot;</b></font>]
         <font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>100
         </b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;@Msg:alert@bug found&quot;</b></font>]
      <font color="#000080"><b>ENDIF    
    
    
    
    GOTO </b></font>Begin
    Reasoning looks good. No need for the second test and comparisons though.

    Trent Jackson
    Last edited by T.Jackson; - 11th June 2008 at 16:16.

  17. #17
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    Ski, is this ridiculous enough? I can go deeper.
    Keep going!!!

    (What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)

    My next submission:
    Code:
    testnumber var word
    onescount var byte
    onescount1 var byte
    loopvar var byte
    
    testnumber = 12345
    
    main:
    for loopvar = 0 to 15 'counts bits set in the testnumber
    if testnumber.0[loopvar] = 1 then
    onescount = onescount + 1
    endif
    next loopvar
    
    testnumber = testnumber >> 1 'shifts the LSB out
    testnumber = testnumber << 1 'shifts a zero bit back in
    
    for loopvar = 0 to 15 'recounts bits set in the testnumber
    if testnumber.0[loopvar] = 1 then
    onescount1 = onescount1 + 1
    endif
    next loopvar
    
    if onescount = onescount1 then lcdout "EVEN" 'if the number of set bits is the same, its even
    if onescount <> onescount1 then lcdout "ODD"
    STOP 'please stop!
    Last edited by skimask; - 11th June 2008 at 16:34.

  18. #18
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Keep going!!!

    (What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)
    Here they call it "Stupiditer II".

    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  19. #19
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    Here they call it "Stupiditer II".
    Rube Goldberg machines...

    http://en.wikipedia.org/wiki/Rube_Goldberg_machine

  20. #20
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Here's another approach:

    Code:
    TestNumber as word
    TestVal as word
    
    If TestNumber = 0 then Zero
    TestVal = 2
    Test:
    	If TestNumber = TestVal Then
    		LCDOUT "Even"
    		EndProgram
    	Else
    		If TestNumber = TestVal - 1 then
    			LCDOUT "Odd"
    			EndProgram
    		Else
    			TestVal = TestVal + 2
    			Test
    		EndiF
    	Endif
    
    Zero:
    	LCDOUT "Zero"
    	EndProgram
    
    EndProgram:
    end

  21. #21
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Keep going!!!

    (What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)

    My next submission:
    Code:
    testnumber var word
    onescount var byte
    onescount1 var byte
    loopvar var byte
    
    testnumber = 12345
    
    main:
    for loopvar = 0 to 15 'counts bits set in the testnumber
    if testnumber.0[loopvar] = 1 then
    onescount = onescount + 1
    endif
    next loopvar
    
    testnumber = testnumber >> 1 'shifts the LSB out
    testnumber = testnumber << 1 'shifts a zero bit back in
    
    for loopvar = 0 to 15 'recounts bits set in the testnumber
    if testnumber.0[loopvar] = 1 then
    onescount1 = onescount1 + 1
    endif
    next loopvar
    
    if onescount = onescount1 then lcdout "EVEN" 'if the number of set bits is the same, its even
    if onescount <> onescount1 then lcdout "ODD"
    STOP 'please stop!
    That submission is void. The method for direct bit testing has already recognized. You were actually in the running for first prize of ($100,000) for your first submission. But unfortunately you have been suspended from the competition for acts plagiarism.

    Naughty, naughty

    Trent Jackson

  22. #22
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by SteveB View Post
    Here's another approach:

    Code:
    TestNumber as word
    TestVal as word
    
    If TestNumber = 0 then Zero
    TestVal = 2
    Test:
    	If TestNumber = TestVal Then
    		LCDOUT "Even"
    		EndProgram
    	Else
    		If TestNumber = TestVal - 1 then
    			LCDOUT "Odd"
    			EndProgram
    		Else
    			TestVal = TestVal + 2
    			Test
    		EndiF
    	Endif
    
    Zero:
    	LCDOUT "Zero"
    	EndProgram
    
    EndProgram:
    end
    Excellent! That's called a recursive method.

    Trent Jackson

  23. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by T.Jackson View Post
    The method for direct bit testing has already recognized.
    Aye lad...but if you'll notice...the direct bit checking is not used to determine the final result! A bit like using a pencil to design a machine to manufacture more pencils.
    This could get fun...

  24. #24
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Aye lad...but if you'll notice...the direct bit checking is not used to determine the final result! A bit like using a pencil to design a machine to manufacture more pencils.
    This could get fun...
    My approach is also somewhat indirect, in that it does not directly check if the number is odd. Rather, it sequentially checks to see if the test number matches a value which is known to be either even or odd.

  25. #25
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by SteveB View Post
    My approach is also somewhat indirect, in that it does not directly check if the number is odd. Rather, it sequentially checks to see if the test number matches a value which is known to be either even or odd.
    You're pretty much doing the same as Sayzer (comparing with known to be odd / even numbers) -- but the way in which you have it configured to call itself repeatedly until the operation has completed is considered to be recursion. However, normally recursive methods perform the work on the subject and not references.

    Trent Jackson

  26. #26
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Another recursive method. However this time it uses the unique feature of Variable Rollover. Also, if nothing is found at the end of checking all possible values for a WORD, the outcome must be Zero.

    Code:
    TestNumber as word
    TestVal as word
    
    For TestVal = 65534 to 2 step -2
    	If (TestNumber + TestVal)= 0 Then
    		LCDOUT "Even"
    		EndProgram
    	Else
    		If (TestNumber + TestVal) + 1 = 0 then
    			LCDOUT "Odd"
    			EndProgram
    		Else
    	Endif
    Next TestVal
    	LCDOUT "Zero"
    EndProgram:
    end
    EDIT: This may be premature, I think I left a hole at the value of "65535". Standby
    Last edited by SteveB; - 11th June 2008 at 17:26.

  27. #27
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by SteveB View Post
    Another recursive method. However this time it uses the unique feature of Variable Rollover. Also, if nothing is found at the end of checking all possible values for a WORD, the outcome must be Zero.

    Code:
    TestNumber as word
    TestVal as word
    
    For TestVal = 65534 to 2 step -2
    	If (TestNumber + TestVal)= 0 Then
    		LCDOUT "Even"
    		EndProgram
    	Else
    		If (TestNumber + TestVal) + 1 = 0 then
    			LCDOUT "Odd"
    			EndProgram
    		Else
    	Endif
    Next TestVal
    	LCDOUT "Zero"
    EndProgram:
    end
    I don't consider that to be recursive. To be recursive means to have a procedure (method in Java's case) -- that repeatedly calls itself until the operation is complete.

    Kinda like getting a pie that you need to cut up into (n) pieces. Instead of cutting out each piece you cut the whole thing repeadedly until you have (n) pieces.

    Trent Jackson

  28. #28
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    OK, This is better. It has to do one final check after the FOR..NEXT loop.

    Code:
    TestNumber as word
    TestVal as word
    
    For TestVal = 65534 to 2 step -2
    	If (TestNumber + TestVal)= 0 Then
    		LCDOUT "Even"
    		EndProgram
    	Else
    		If (TestNumber + TestVal) + 1 = 0 then
    			LCDOUT "Odd"
    			EndProgram
    		Else
    	Endif
    Next TestVal
    If (TestNumber + TestVal) - 1 = 0 then
           LCDOUT "Odd"
           EndProgram
    Else
           LCDOUT "Zero"
    Endif
    EndProgram:
    end
    Last edited by SteveB; - 11th June 2008 at 17:35.

  29. #29
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Recursive example ...

    Code:
    public int divideBy(int n)
    {
       num = num / n
       if(num > 0)
          divideBy(n);
    }
    It keeps calling itself until we have arrived at something. In PBP you would of course need to exchange the "public int divideBy(int n)" with a label and swap "divideBy(n)" with a goto.

    Trent Jackson

  30. #30
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89


    Did you find this post helpful? Yes | No

    Default

    Hi Trent, I think...

    There'r Out of 10's methods. You want's onlyup to 10th.
    So should Listing now.

    .

Similar Threads

  1. Dynamic USB Serial Number (PIC18F4550)
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th July 2009, 17:03
  2. dec number to show on lcd (maths)
    By savnik in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th August 2007, 19:18
  3. Working with the random number generator
    By kwelna in forum mel PIC BASIC
    Replies: 9
    Last Post: - 16th January 2007, 17:50
  4. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts