AJ Riley - 29 days ago 4

Vb.net Question

"Write a program which reads in a start and an end value. The program then stores all the even numbers between these two values (inclusive) in an array. The user is then asked to select a number (n), the program should output the nth even number"

`Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click`

number1 = InputBox("Enter first number")

number2 = InputBox("enter second number")

Any guidance on this would be much appreciated, I'm completely lost.

Answer

Ok, the edit is making more sense to me now. You were on the right track getting the first three inputs. Then we neeed to do 2 things to our inputs:

1) Get the even numbers within the range the user has given us

2) Return the nth term if it exists

I would approach the problem like this:

```
'Get our inputs
Dim number1 As Integer = CInt(InputBox("Enter first number"))
Dim number2 As Integer = CInt(InputBox("Enter second number"))
Dim nthTerm As Integer = CInt(InputBox("Enter Nth Term"))
Dim evenNumbers As New List(Of Integer)
'Now, we want to get a list of all the even numbers within n1 to n2 range
For i As Integer = number1 To number2
'if the number divided by 2 has a remainder of 0, then it's an even number
If i Mod 2 = 0 Then evenNumbers.Add(i)
Next
'Now that we have all the even #s, try to return the nth one as long as it exists
Try
'We substract 1 from the nthTerm entered by used to account for list's 0-based index
MsgBox(evenNumbers(nthTerm - 1).ToString)
Catch ex As Exception
MsgBox("Nth Term out of bounds")
End Try
```

Source (Stackoverflow)

Comments