Is Number Odd?


Closed Thread
Results 1 to 30 of 30

Thread: Is Number Odd?

Hybrid View

  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

    .

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