Reputation: 117
I have a Visual Basic program that expects two integers from the user.
I need tobe able to display the sum of the even numbers between the two integers entered by the user. If the user's entry is even, it should be included in the sum.
I am having trouble figuring out the algorithm.
Here is my code:
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtFirstNum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFirstNum.KeyPress
' allows the text box to accept only numbers, and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub txtSecondNum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSecondNum.KeyPress
' allows numbers and Backspace only
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' display the sum of even numbers
' declarations
Dim intFirstNum As Integer
Dim intSecondNum As Integer
' convert string to number with TryParse method
Integer.TryParse(txtFirstNum.Text, intFirstNum)
Integer.TryParse(txtSecondNum.Text, intSecondNum)
' find the even numbers
If intFirstNum Mod 2 = 0 Then
ElseIf intFirstNum Mod 2 = 1 Then
End If
' add the even numbers
' display the result
End Sub
End Class
How can I fix this?
Upvotes: 1
Views: 7058
Reputation: 415725
If you know the first number is even by checking that at the start, you don't have to check the modulus of every digit along the way, and can just increment by 2s:
Public Function SumEvenNumbers(ByVal Start As Integer, ByVal End As Integer) As Integer
If Start Mod 2 = 1 Then Start += 1
Dim Result As Integer = 0
While Start <= End
Result += Start
Start += 2
End While
Return Result
End Function
And just for fun, here's an example that uses linq and Enumerable.Range:
Public Function SumEvenNumbers(ByVal Start As Integer, ByVal End As Integer) As Integer
If Start Mod 2 = 1 Then Start += 1
Return Enumerable.Range(Start, End - Start).Sum(Function(i) i Mod 2 == 0)
End Function
But the best answer is the one that's already posted, and just uses Math to reduce it all to a fixed average ( O(1), rather than O(n) ).
Upvotes: 1
Reputation: 700302
You can calculate how many even numbers there are, and from that calculate the sum:
' adjust the numbers to the closest even number
If (intFirstNum mod 2) = 1 Then intFirstNum += 1
If (intSecondNum mod 2) = 1 Then intSecondNum -= 1
' determine how many even numbers there are in the range
Dim count As Integer = (intSecondNum - intFirstNum) \ 2 + 1
' the sum is the average times the count
Dim sum As Integer = (intFirstNum + intSecondNum) * count \ 2
The TryParse
methods returns a boolean, which tells you if it was possible to parse the string. You shouldn't ignore the return value, so use it as:
If Integer.TryParse(txtFirstNum.Text, intFirstNum) Then
' yes, it was a number
End If
Upvotes: 2
Reputation: 967
Public Function GetEvenNumberSum(min As Integer, max As Integer) As Integer
Dim sum As Integer = 0
For i As Integer = min To max
If i Mod 2 = 0 Then
sum += i
End If
Next
Return sum
End Function
Upvotes: -1