440,071 Members | 1,244 Online Need help? Post your question and get tips & solutions from a community of 440,071 IT Pros & Developers. It's quick & easy.

# How to distinguish odd / even numbers and sumthem

 P: n/a I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number 23578 the label would say: Sumof odd number is: 15 Sum of even number is: 10 any ideas? thanks Jan 24 '07 #1
17 Replies

 P: n/a This sounds like a homework problem? If so, you should really work this out on your own and not post here. It's really hard to imagine a real world application wanting to do this, and a real world programmer not knowing how to accomplish this... "Ron" I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number 23578 the label would say: Sumof odd number is: 15 Sum of even number is: 10 any ideas? thanks Jan 24 '07 #2

 P: n/a Plus think "in English" how you would explain step by step to someone how to do this. Then tell the same to the computer. For example the first step would be likely to extract each digit for processing (until the total length is reached), once you have a digit what would you do and so on... Welcome to programming... "Marina Levit [MVP]" >I want to write a program that will accept a number in a textbox forexample 23578 and then in a label will display the sum of the odd andeven number like this...the textbox containsthe number 23578the label would say:Sumof odd number is: 15Sum of even number is: 10any ideas?thanks Jan 24 '07 #3

 P: n/a "Ron" I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number 23578 the label would say: Sumof odd number is: 15 Sum of even number is: 10 Check out the 'Mid' function, the 'For' loop, and the 'Mod' statement. -- M S Herfried K. Wagner M V P V B Jan 24 '07 #4

 P: n/a Well that will get him a C- :-) "Lost" >I want to write a program that will accept a number in a textbox forexample 23578 and then in a label will display the sum of the odd andeven number like this...the textbox containsthe number 23578the label would say:Sumof odd number is: 15Sum of even number is: 10any ideas?thanks Dim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens) -- Modo vincis, modo peris Jan 24 '07 #5

 P: n/a In my day, we had the smarts to cheat on our homework assignments in a place that wasn't 100% public and archived for all history with a search tool that's accessed daily by billions of people. Young people these days. Sheesh. -- Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C# http://www.coversant.net/blogs/cmullins "Ron" I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number 23578 the label would say: Sumof odd number is: 15 Sum of even number is: 10 any ideas? thanks Jan 25 '07 #6

 P: n/a Dim intTotEvens As Integer Dim intTotOdds As Integer Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg 23578 For intIndex As Integer = 0 To strNumber.Length - 1 Dim strTemp As String = strNumber.SubString(intIndex, 1) If ("02468").IndexOf(strTemp) >= 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString() Does that at least get me a C+? :) This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" "Ron" >>I want to write a program that will accept a number in a textbox forexample 23578 and then in a label will display the sum of the odd andeven number like this...the textbox containsthe number 23578the label would say:Sumof odd number is: 15Sum of even number is: 10any ideas?thanks Dim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens) --Modo vincis, modo peris Jan 25 '07 #7

 P: n/a I guess I sort of stuck my foot in my mouth on that one... now I have to post something :-) Glad "Lost" took it with humor. What I love about these homework assignments is that they create small topics from which solutions can be discussed. Questions like "how do I write a video game" isn't quite going to be solved by each of us submitting our collision detection routine. I'd suggest a couple of things. First the routine should be written independently of the source of input so I wouldn't grab the data out of the textbox but would require that to be passed to the routine. I opted to display the values directly into the labels though. The reason is the extra code needed to initialize them (outside of the routine) and to pass them by reference just obfuscates things (since (I doubt) this is going to be part of a reusable library.) The other was testing the characters as "strings" is problematic. The solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it must have been "1,3,5,7 or 9" and I don't know the user didn't key in "ab383994*0". I decided to add some functionality that I might be able to use in the future by creating IsEven() and IsOdd() functions. While not necessary I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know. Uh, let's see I opted for the Int32 declaration (not trying to start a fight but I prefer it too). And I used TryParse so I can determine when non-digits were entered and I can ignore them. So here is mine: ' upon some click event or something ValueCounter(txtInput.Text.Trim()) Private Sub Test7(ByVal sInput As String) Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 Dim bIsDigit As Boolean Dim nValue As Int32 = 0 For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() End Sub Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function "Stephany Young" = 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString() Does that at least get me a C+? :) This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" Well that will get him a C- :-)"Lost" >"Ron"

 P: n/a You're really having fun today Tom! ;) Shouldn't it be: ' upon some click event or something Test7(txtInput.Text.Trim()) But now we're really cooking with gas: Dim oae As New OddsAndEvens If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then lblEven.Text = "Sum of even numbers is: " & oae.TotalOfEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString() Else MessageBox.Show("Supplied value is not a valid value") End If Public Class OddsAndEvens Private m_odds As Int32 Private m_evens As Int32 Public ReadOnly Property TotalOfOdds() As Int32 Get Return m_odds End Get End Property Public ReadOnly Property TotalOfEvens() As Int32 Get Return m_evens End Get End Property Public Function TotalOddsAndEvens(value As String) As Boolean m_odds = 0 m_evens = 0 Dim nValue As Int32 = 0 For i As Integer = 0 To value.Length - 1 If Int32.TryParse(sInput.Substring(i, 1), nValue) If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If Else Return False End If Next Return False End Sub Public Shared Function IsEven(ByVal num As Int32) As Boolean Return (num Mod 2 = 0) End Function Public Shared Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function End Class "Tom Leylan" I guess I sort of stuck my foot in my mouth on that one... now I have topost something :-) Glad "Lost" took it with humor. What I love aboutthese homework assignments is that they create small topics from whichsolutions can be discussed. Questions like "how do I write a video game"isn't quite going to be solved by each of us submitting our collisiondetection routine. I'd suggest a couple of things. First the routine should be written independently of the source of input so I wouldn't grab the data out of the textbox but would require that to be passed to the routine. I opted to display the values directly into the labels though. The reason is the extra code needed to initialize them (outside of the routine) and to pass them by reference just obfuscates things (since (I doubt) this is going to be part of a reusable library.) The other was testing the characters as "strings" is problematic. The solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it must have been "1,3,5,7 or 9" and I don't know the user didn't key in "ab383994*0". I decided to add some functionality that I might be able to use in the future by creating IsEven() and IsOdd() functions. While not necessary I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know. Uh, let's see I opted for the Int32 declaration (not trying to start a fight but I prefer it too). And I used TryParse so I can determine when non-digits were entered and I can ignore them. So here is mine: ' upon some click event or something ValueCounter(txtInput.Text.Trim()) Private Sub Test7(ByVal sInput As String) Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 Dim bIsDigit As Boolean Dim nValue As Int32 = 0 For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() End Sub Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function "Stephany Young" Dim intTotEvens As Integer Dim intTotOdds As Integer Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg23578 For intIndex As Integer = 0 To strNumber.Length - 1 Dim strTemp As String = strNumber.SubString(intIndex, 1) If ("02468").IndexOf(strTemp) >= 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()Does that at least get me a C+? :)This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" >Well that will get him a C- :-)"Lost" I want to write a program that will accept a number in a textbox for>example 23578 and then in a label will display the sum of the odd and>even number like this...>>the textbox containsthe number 23578>the label would say:>Sumof odd number is: 15>Sum of even number is: 10>>any ideas?>thanksDim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens) --Modo vincis, modo peris Jan 25 '07 #9

 P: n/a I see I forgot to edit my function call... in my test routines I named it Test7() but I renamed the thing as I posted. And yes I like it as a class. Is something up with the TotalOddsAndEvens method? It's returning False all the time and I'm not sure that a non-digit in the string should make it fail. And since the class is named OddsAndEvens I might suggest "Total()" is a good enough name for the method. And this is why I like these simple examples. Note how you grouped the private variables at the top of the class? It is a very common style and used in many books, probably in the MS docs as well. I long ago stopped doing this and opted for a style which came out of Delphi as I recall. I declare the private instance variable and then the property. That way I can see things on a "per property" basis and easily determine whether I have remembered to generate the property, if it is public/private and whether it is read-only. It's only a style but I've grown to love it... so it would look like this: Private m_odds As Int32 Public ReadOnly Property TotalOfOdds() As Int32 Get Return m_odds End Get End Property Private m_evens As Int32 Public ReadOnly Property TotalOfEvens() As Int32 Get Return m_evens End Get End Property "Stephany Young" >I guess I sort of stuck my foot in my mouth on that one... now I have topost something :-) Glad "Lost" took it with humor. What I love aboutthese homework assignments is that they create small topics from whichsolutions can be discussed. Questions like "how do I write a video game"isn't quite going to be solved by each of us submitting our collisiondetection routine.I'd suggest a couple of things. First the routine should be writtenindependently of the source of input so I wouldn't grab the data out ofthe textbox but would require that to be passed to the routine. I optedto display the values directly into the labels though. The reason is theextra code needed to initialize them (outside of the routine) and to passthem by reference just obfuscates things (since (I doubt) this is goingto be part of a reusable library.)The other was testing the characters as "strings" is problematic. Thesolutions tended to insist if a character wasn't "0,2,4,6 or 8" then itmust have been "1,3,5,7 or 9" and I don't know the user didn't key in"ab383994*0".I decided to add some functionality that I might be able to use in thefuture by creating IsEven() and IsOdd() functions. While not necessaryI'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.Uh, let's see I opted for the Int32 declaration (not trying to start afight but I prefer it too). And I used TryParse so I can determine whennon-digits were entered and I can ignore them.So here is mine: ' upon some click event or something ValueCounter(txtInput.Text.Trim()) Private Sub Test7(ByVal sInput As String) Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 Dim bIsDigit As Boolean Dim nValue As Int32 = 0 For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() End Sub Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function "Stephany Young" > Dim intTotEvens As Integer Dim intTotOdds As Integer Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg23578 For intIndex As Integer = 0 To strNumber.Length - 1 Dim strTemp As String = strNumber.SubString(intIndex, 1) If ("02468").IndexOf(strTemp) >= 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()Does that at least get me a C+? :)This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" >>I want to write a program that will accept a number in a textbox for>>example 23578 and then in a label will display the sum of the odd and>>even number like this...>>>>the textbox containsthe number 23578>>the label would say:>>Sumof odd number is: 15>>Sum of even number is: 10>>>>any ideas?>>thanks>Dim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer> intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)>>>--Modo vincis, modo peris Jan 25 '07 #10

 P: n/a Yes, you're right. The final Return False should be Return True. Yes, it should fail (return False) if any character cannot be parsed as in integer. If you supply a value such as 123A456 then that is not a number and therfore is out of context for the method. Remember that the OP stated that the user was entering a 'number'. Of course one could argue that such a function should handle any string that might represent a number including decimal points, thousands separators, currency symbols, signs, etc., and, if one was so inclined could easily expand the functionality to be more 'fuzzy'. My preference, in a class, is to group the private members so that they are all in one place. "Tom Leylan" I see I forgot to edit my function call... in my test routines I named itTest7() but I renamed the thing as I posted. And yes I like it as a class. Is something up with the TotalOddsAndEvens method? It's returning False all the time and I'm not sure that a non-digit in the string should make it fail. And since the class is named OddsAndEvens I might suggest "Total()" is a good enough name for the method. And this is why I like these simple examples. Note how you grouped the private variables at the top of the class? It is a very common style and used in many books, probably in the MS docs as well. I long ago stopped doing this and opted for a style which came out of Delphi as I recall. I declare the private instance variable and then the property. That way I can see things on a "per property" basis and easily determine whether I have remembered to generate the property, if it is public/private and whether it is read-only. It's only a style but I've grown to love it... so it would look like this: Private m_odds As Int32 Public ReadOnly Property TotalOfOdds() As Int32 Get Return m_odds End Get End Property Private m_evens As Int32 Public ReadOnly Property TotalOfEvens() As Int32 Get Return m_evens End Get End Property "Stephany Young" You're really having fun today Tom! ;)Shouldn't it be: ' upon some click event or something Test7(txtInput.Text.Trim())But now we're really cooking with gas: Dim oae As New OddsAndEvens If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then lblEven.Text = "Sum of even numbers is: " &oae.TotalOfEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString() Else MessageBox.Show("Supplied value is not a valid value") End If Public Class OddsAndEvens Private m_odds As Int32 Private m_evens As Int32 Public ReadOnly Property TotalOfOdds() As Int32 Get Return m_odds End Get End Property Public ReadOnly Property TotalOfEvens() As Int32 Get Return m_evens End Get End Property Public Function TotalOddsAndEvens(value As String) As Boolean m_odds = 0 m_evens = 0 Dim nValue As Int32 = 0 For i As Integer = 0 To value.Length - 1 If Int32.TryParse(sInput.Substring(i, 1), nValue) If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If Else Return False End If Next Return False End Sub Public Shared Function IsEven(ByVal num As Int32) As Boolean Return (num Mod 2 = 0) End Function Public Shared Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function End Class "Tom Leylan" >>I guess I sort of stuck my foot in my mouth on that one... now I have topost something :-) Glad "Lost" took it with humor. What I love aboutthese homework assignments is that they create small topics from whichsolutions can be discussed. Questions like "how do I write a video game"isn't quite going to be solved by each of us submitting our collisiondetection routine.I'd suggest a couple of things. First the routine should be writtenindependently of the source of input so I wouldn't grab the data out ofthe textbox but would require that to be passed to the routine. I optedto display the values directly into the labels though. The reason isthe extra code needed to initialize them (outside of the routine) and topass them by reference just obfuscates things (since (I doubt) this isgoing to be part of a reusable library.)The other was testing the characters as "strings" is problematic. Thesolutions tended to insist if a character wasn't "0,2,4,6 or 8" then itmust have been "1,3,5,7 or 9" and I don't know the user didn't key in"ab383994*0".I decided to add some functionality that I might be able to use in thefuture by creating IsEven() and IsOdd() functions. While not necessaryI'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.Uh, let's see I opted for the Int32 declaration (not trying to start afight but I prefer it too). And I used TryParse so I can determine whennon-digits were entered and I can ignore them.So here is mine: ' upon some click event or something ValueCounter(txtInput.Text.Trim()) Private Sub Test7(ByVal sInput As String) Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 Dim bIsDigit As Boolean Dim nValue As Int32 = 0 For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() End Sub Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function "Stephany Young" = 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()Does that at least get me a C+? :)This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" "Lost" "Ron" >>>>I want to write a program that will accept a number in a textbox for>>>example 23578 and then in a label will display the sum of the odd and>>>even number like this...>>>>>>the textbox containsthe number 23578>>>the label would say:>>>Sumof odd number is: 15>>>Sum of even number is: 10>>>>>>any ideas?>>>thanks>>>Dim strNumber As String> Dim strTemp As String> Dim intIndex As Integer> Dim intTotEvens As Integer> Dim intTotOdds As Integer>>> intTotOdds = 0> intTotEvens = 0> strNumber = Trim(TextBox1.Text) ' contains input, eg 23578> For intIndex = 1 To Len(strNumber)> strTemp = Mid(strNumber, intIndex, 1)> If InStr("02468", strTemp) 0 Then> intTotEvens = intTotEvens + Val(strTemp)> Else> intTotOdds = intTotOdds + Val(strTemp)> End If> Next intIndex> Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)> Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)>>>>>>>-->Modo vincis, modo peris>> Jan 25 '07 #11

 P: n/a thanks for all of yur posts everone. I am not looking for a way to cheat on this assignmwent, just for some help. And I found it, the Trim() was what I was looking for, I was not sure how to divide the number into its individual numbers. thanks. On Jan 24, 10:13 pm, "Stephany Young" I'd suggest a couple of things. First the routine should be writtenindependently of the source of input so I wouldn't grab the data out ofthe textbox but would require that to be passed to the routine. I optedto display the values directly into the labels though. The reason isthe extra code needed to initialize them (outside of the routine) and topass them by reference just obfuscates things (since (I doubt) this isgoing to be part of a reusable library.) >The other was testing the characters as "strings" is problematic. Thesolutions tended to insist if a character wasn't "0,2,4,6 or 8" then itmust have been "1,3,5,7 or 9" and I don't know the user didn't key in"ab383994*0". >I decided to add some functionality that I might be able to use in thefuture by creating IsEven() and IsOdd() functions. While not necessaryI'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.Uh, let's see I opted for the Int32 declaration (not trying to start afight but I prefer it too). And I used TryParse so I can determine whennon-digits were entered and I can ignore them. >So here is mine: > ' upon some click event or something ValueCounter(txtInput.Text.Trim()) > Private Sub Test7(ByVal sInput As String) > Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 > Dim bIsDigit As Boolean Dim nValue As Int32 = 0 > For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next > lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() > End Sub > Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function > Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function >"Stephany Young" > Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg23578 >> For intIndex As Integer = 0 To strNumber.Length - 1 Dim strTemp As String = strNumber.SubString(intIndex, 1) If ("02468").IndexOf(strTemp) >= 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next >> Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() >> Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString() >>Does that at least get me a C+? :) >>This should grade higher: >> Dim _evens As Integer Dim _odds As Integer >> For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next >> Label1.Text = "Sum of odd numbers is: " & _odds.ToString() >> Label2.Text = "Sum of even numbers is: " & _evens.ToString() >>"Tom Leylan" >>"Lost" >>>>>I want to write a program that will accept a number in a textbox for>>example 23578 and then in a label will display the sum of the odd and>>even number like this... >>>>>>the textbox containsthe number 23578>>the label would say:>>Sumof odd number is: 15>>Sum of even number is: 10 >>>>>>any ideas?>>thanks >>>>Dim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer >>>> intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens) >>>>--Modo vincis, modo peris- Hide quoted text -- Show quoted text - Jan 25 '07 #12

 P: n/a Well Ron... as you know from reading the docs (I kid, I kid) the Trim() isn't likely to have any affect whatsoever in 99% of the cases. The user would have to type in leading or trailing spaces for it to make any impact at all. I do it as a habit to simply shorten the loop and imbedded blanks would remain. If you're really not looking for the code to be written for you then one alternative is to actually post your code and get it reviewed. And if really want to know how to program then I'll suggest that when you're assigned 1 programming assignment you undertake to write 10 programs. "Ron"

 P: n/a "Stephany Young"

 P: n/a "Stephany Young"

 P: n/a Tom, Thanks for this little bit of code. I've been trying to work out in my own mind how to create a class. Bruce "Tom Leylan" I guess I sort of stuck my foot in my mouth on that one... now I have topost something :-) Glad "Lost" took it with humor. What I love aboutthese homework assignments is that they create small topics from whichsolutions can be discussed. Questions like "how do I write a video game"isn't quite going to be solved by each of us submitting our collisiondetection routine. I'd suggest a couple of things. First the routine should be written independently of the source of input so I wouldn't grab the data out of the textbox but would require that to be passed to the routine. I opted to display the values directly into the labels though. The reason is the extra code needed to initialize them (outside of the routine) and to pass them by reference just obfuscates things (since (I doubt) this is going to be part of a reusable library.) The other was testing the characters as "strings" is problematic. The solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it must have been "1,3,5,7 or 9" and I don't know the user didn't key in "ab383994*0". I decided to add some functionality that I might be able to use in the future by creating IsEven() and IsOdd() functions. While not necessary I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know. Uh, let's see I opted for the Int32 declaration (not trying to start a fight but I prefer it too). And I used TryParse so I can determine when non-digits were entered and I can ignore them. So here is mine: ' upon some click event or something ValueCounter(txtInput.Text.Trim()) Private Sub Test7(ByVal sInput As String) Dim nEvens As Int32 = 0 Dim nOdds As Int32 = 0 Dim bIsDigit As Boolean Dim nValue As Int32 = 0 For i As Integer = 0 To (sInput.Length - 1) bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue) If bIsDigit Then If IsEven(nValue) Then nEvens += nValue ElseIf IsOdd(nValue) Then nOdds += nValue End If End If Next lblEven.Text = "Sum of even numbers is: " + nEvens.ToString() lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString() End Sub Public Function IsEven(ByVal num As Int32) As Boolean Return ((num Mod 2) = 0) End Function Public Function IsOdd(ByVal num As Int32) As Boolean Return (Not IsEven(num)) End Function "Stephany Young" Dim intTotEvens As Integer Dim intTotOdds As Integer Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg23578 For intIndex As Integer = 0 To strNumber.Length - 1 Dim strTemp As String = strNumber.SubString(intIndex, 1) If ("02468").IndexOf(strTemp) >= 0 Then intTotEvens += Integer.Parse(strTemp) Else intTotOdds += Integer.Parse(strTemp) End If Next Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString() Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()Does that at least get me a C+? :)This should grade higher: Dim _evens As Integer Dim _odds As Integer For _i As Integer = 0 To TextBox1.Text.Length - 1 Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1)) If _v Mod 2 = 0 Then _evens += _v Else _odds += _v End If Next Label1.Text = "Sum of odd numbers is: " & _odds.ToString() Label2.Text = "Sum of even numbers is: " & _evens.ToString() "Tom Leylan" >Well that will get him a C- :-)"Lost" I want to write a program that will accept a number in a textbox for>example 23578 and then in a label will display the sum of the odd and>even number like this...>>the textbox containsthe number 23578>the label would say:>Sumof odd number is: 15>Sum of even number is: 10>>any ideas?>thanksDim strNumber As String Dim strTemp As String Dim intIndex As Integer Dim intTotEvens As Integer Dim intTotOdds As Integer intTotOdds = 0 intTotEvens = 0 strNumber = Trim(TextBox1.Text) ' contains input, eg 23578 For intIndex = 1 To Len(strNumber) strTemp = Mid(strNumber, intIndex, 1) If InStr("02468", strTemp) 0 Then intTotEvens = intTotEvens + Val(strTemp) Else intTotOdds = intTotOdds + Val(strTemp) End If Next intIndex Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds) Label2.Text = "Sum of even numbers is: " + Str(intTotEvens) --Modo vincis, modo peris Jan 25 '07 #16

 P: n/a But only my husband gets to ring my bell ;) "Bruce W. Darby" "Stephany Young" End Class But Stephany, since this is just a homework assignment, you can only End Class when the bell rings!!! :) Jan 25 '07 #17

 P: n/a BTW, The help you provided me with my little program for work was greatly appreciated. It allowed me to advance that project to the point where I was able to use it at work to show the techs that they don't have to individually compress all those folders. During the compression, the interface was non-responsive, however, so I'm going to be trying to put the compression portion of the module into a thread of it's own. I'm kinda nervous, but I'm trying to work out a way to use the BackgroundWorker, so I'll probably be back asking questions again soon. :) Thanks all, Bruce "Stephany Young" >"Stephany Young" > End Class But Stephany, since this is just a homework assignment, you can only EndClass when the bell rings!!! :) Jan 25 '07 #18

### This discussion thread is closed

Replies have been disabled for this discussion. 