Important alert: (current site time 7/16/2013 1:32:46 AM EDT)
 

VB icon

[ Odd or Even? ]

Email
Submitted on: 8/4/2003 6:46:30 PM
By: JamesJD  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 22445
author picture
(About the author)
 
     This simple little program will work out if the number entered is odd or even.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: [ Odd or Even? ]
' Description:This simple little program will work out if the number entered is odd or even.
' By: JamesJD
'
' Inputs:One TextBox, one CommandButton, and two Labels. (You just need to keep their default names).
'
' Returns:If the number entered is odd or even.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=47403&lngWId=1'for details.'**************************************

Private Sub Command1_Click()
Dim a
a = Text1.Text
Dim b
b = a / 2
Dim c
c = Int(b)
If b = c Then
 Label1.Caption = "Odd: False"
 Label2.Caption = "Even: True"
 Text1.Text = ""
 Text1.SetFocus
 
Else
 Label1.Caption = "Odd: True"
 Label2.Caption = "Even: False"
 Text1.Text = ""
 Text1.SetFocus
 
End If
End Sub


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
8/4/2003 8:25:45 PMBen Gray

alright i suppose, but very slow and limited to int.

Use:


Dim lngCheckNumber As Long
lngCheckNumber = 3 'change this number
If lngCheckNumber Mod 2 = 1 Then
MsgBox "Odd Number"
Else
MsgBox "Even Number"
End If

(If this comment was disrespectful, please report it.)

 
8/4/2003 11:02:26 PMMitch Foral

It would even be more efficient if you had something like this:

Public Function OddorEven(Number as Integer) as String
If Number Mod 2 = 1 Then
OddorEven = "Odd"
Else
OddorEven = "Even"
End If
End Function

Private Sub Command1_Click()
MsgBox OddorEven(CInt(Text1.Text))
End Sub
(If this comment was disrespectful, please report it.)

 
8/5/2003 6:48:39 AMEspen Skjervold

or even simpler still:

Public Function isEven(Nr As Double) As Boolean
If Nr Mod 2 = 0 Then isEven = True
End Function

To use it you could say:

If isEven(x) Then 'x is the nr to check
MsgBox "Even!"
Else
MsgBox "not even!"
End If


(If this comment was disrespectful, please report it.)

 
8/5/2003 10:26:36 AM

Then if we're talking about slow... I'd still prefer to go with an AND instead of a Mod

x AND 1 results in 1 if x is odd, 0 if x is even...
(If this comment was disrespectful, please report it.)

 
8/5/2003 11:09:43 AMPaulo S. Silva Jr

Well... just to keep this going...

If we are talking about functions why not oneliner function...

Function IsEven(Number as Integer) as Boolean
IsEven = Not CBool(Number Mod 2)
End Function
---------------------------------
.NET Version

Function IsEven(Number as Integer) as Boolean
Return (Not CBool(Number Mod 2))
End Function

Regards,

MstrControl@yahoo.com
(If this comment was disrespectful, please report it.)

 
9/20/2003 7:36:16 PMKenKnutson

So many ways to do so simple a thing.
(If this comment was disrespectful, please report it.)

 
10/10/2003 12:18:27 PMTechni Rei Myoko

public function iseven(number as long)as boolean
iseven = number mod 2 = 0
end function

public function isodd(number as long)as boolean
isodd = not iseven(number)
end function
(If this comment was disrespectful, please report it.)

 
11/25/2003 2:39:39 AMAlan Carstens

Another very simple way is as follows:

Private Function IsEven(ByVal Number As Integer) As Boolean
If Number / 2 = Number \ 2 Then
Return True
else
Return False
End If
End Function

(If this comment was disrespectful, please report it.)

 
5/21/2004 4:08:04 AMRobert Kaltenbach

MsgBox IIf(Text1.Text And 1, "Odd", "Even") & " Number"
(If this comment was disrespectful, please report it.)

 
11/3/2005 11:27:57 PMAnthony Dwyer

but the versions using "mod" or "and" can only handle a long. to test anything larger than 2147483648, the easiest (but definitely not most efficient) way is to convert it to a string and test the character, i.e.
function IsOdd(byval dNum as double) as boolean

Dim sNum as string
sNum = Right$(trim$(str%(dNum, 1)))

Select Case sNum
Case "2", "4", "6", "8", "0"
return false
case else
return true
End Select

end function

anyone know a faster (binary) way to do this on large numbers?



(If this comment was disrespectful, please report it.)

 
5/9/2006 8:19:32 PMsubjectz

MsgBox IIf(Right("12345", 1) Mod 2 = 0, True, False)
(If this comment was disrespectful, please report it.)

 
5/14/2007 3:15:11 AMSentinel

I used to take a similar approach in discovering leap years.
If yr = Int(yr/4)*4 then it is a leap year, else it isnt.
If you needed to, you could test for 400 if the previous statement is true.
(If this comment was disrespectful, please report it.)

 
8/29/2007 11:08:04 PMMatt

""Robert Kaltenbach

MsgBox IIf(Text1.Text And 1, "Odd", "Even") & " Number"
""

i think that is the best example here. it goes up to 20 digets and is by far the simplest and shortest expression here. thanks :)
(If this comment was disrespectful, please report it.)

 
11/20/2007 5:12:19 AMansari g farid

GOOD
(If this comment was disrespectful, please report it.)

 
4/4/2008 5:42:02 PMTyler

Or instead of the button click: you could simply make it so if Textbox1 text changes, then it will figure it out, such as:


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "" Then
Label1.Text = "Odd: False"
Label2.Text = "Even: False"
Else
Dim a
a = TextBox1.Text
Dim b

b = a / 2
Dim c
c = Int(b)
If b = c Then
Label1.Text = "Odd: False"
Label2.Text = "Even: True"

Else
Label1.Text = "Odd: True"
Label2.Text = "Even: False"

End If


End If
End Sub
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.