Odd and Even sums

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
So I'm trying to write an application that will allow a user to enter a series of integers such as : 45, 4, 5, 12, 10.

The application will need to find the sum of only the odd numbers then find the sum of only the even numbers. I have tried this in so many ways for the past few days my head hurts.

I figure out how to tell if a number is odd or even by using the mod statement; If myNum Mod 2 = 1 then return odd else even.

However to go through the series of numbers, grab only the odd ones for examples then add them all together has me stuck.
 
Well, will that allow for 1 textbox.text?

Basically the GUI is as such.

1 textbox.text - the user can enter the numbers such as 3,45,40

1 oddButton
1 evenButton.

1 textbox.text - to show the results.


Or must I have as many textbox's for numbers to enter.
Such as
textbox1.text to type 45 in
textbox2.text to type 3 in.
..etc
 
VB.NET:
dim oddTotal as integer = 0

If myNum Mod 2 = 1 then
    oddTotal += myNum 
end if

Also, tried this and this will simply display the odd number.

For instance if I enter 5 and click sum odd, it gives me 5. If I enter 55, it gives me 55.. I need it to add all sums together.

Example:

The end user enters the following integers: 45, 2, 34, 7, 55, 90, 32.
The sum of the odd integers should be 107.
The sum of the even integers should be 158
 
try this (attached)
 

Attachments

  • WindowsApplication5.zip
    12.2 KB · Views: 41
Last edited by a moderator:
controls: a button, a textbox, a label.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim numbers() As String = TextBox1.Text.Split(","c)
    Dim odd, even, i As Integer
    For Each number As String In numbers
        If Integer.TryParse(number, i) Then
            If i Mod 2 = 0 Then
                even += i
            Else
                odd += i
            End If
        End If
    Next
    Label1.Text = String.Format("odd numbers sum {0}, even numbers sum {1}", odd, even)
End Sub
 
Hmm, it looked odd and nothing would really open up for me.

I'm using Visual Basic 2005 Express edition
 
controls: a button, a textbox, a label.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim numbers() As String = TextBox1.Text.Split(","c)
    Dim odd, even, i As Integer
    For Each number As String In numbers
        If Integer.TryParse(number, i) Then
            If i Mod 2 = 0 Then
                even += i
            Else
                odd += i
            End If
        End If
    Next
    Label1.Text = String.Format("odd numbers sum {0}, even numbers sum {1}", odd, even)
End Sub

Oh damn that worked! Now I need to spend the time learning how you did it and where I went wrong. I can't thank you enough! You really covered my butt!
 
Back
Top