Is Number Odd?


Closed Thread
Results 1 to 30 of 30

Thread: Is Number Odd?

Hybrid View

  1. #1
    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

    .

  2. #2
    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

  3. #3
    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

    .

  4. #4
    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?

  5. #5
    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

  6. #6
    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!

  7. #7
    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

  8. #8
    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

  9. #9
    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.

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

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