Results 1 to 4 of 4

Thread: Odd/even numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Posts
    4

    Post

    Does anybody have any good code on how to determine whether a number is even or odd? I came up with some simplistic code using a For Next loop, but im looking for something better. Thanx a lot.

  2. #2
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Post

    Use Mod:

    -------------
    If Number Mod 2 = 0 then ' Number is even
    Else ' Number is odd
    End If
    -------------



  3. #3
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    This gives you an idea of how to determine whether a number is even or odd.

    Code:
            Dim mlimit As Integer
            Dim num As Integer
            mlimit = 20
            
            Case EVEN
                For num = 0 To mlimit 
                    If num Mod 2 = 0 Then
                        If num > 0 Then Debug.Print num;
                    End If
                Next num
                
            Case ODD
                For num = 0 To mlimit 
                    If num Mod 2 = 1 Then
                        If num > 0 Then Debug.Print num;
                    End If
                Next num
    Hope this helps.

    Ruchi

    [This message has been edited by Ruchi (edited 01-08-2000).]

  4. #4
    Lively Member
    Join Date
    Jul 1999
    Posts
    99

    Post

    You can also use this function which returns True if the number is even, False if it is odd.

    Function IsEven(iEven as Long) as Boolean
    IsEven = (iEven mod 2 = 0)
    End Function

Posting Permissions

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



Click Here to Expand Forum to Full Width