Im working on an app that takes two numbers(lower bound and upperbound)and determines all the prime numbers within the specific bounds, inclusive.

Public Class primeNumber

    Private Sub calculatePrimesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculatePrimesButton.Click
        'declare variables
        Dim N As Long
        Dim LowerBound As Integer
        Dim UpperBound As Integer

        Dim i As Integer = 0
'user input
        LowerBound = Val(lowerboundTextBox1.Text)
        UpperBound = Val(upperboundTextBox2.Text)
 'display error message when user inputs an upperbound that is less than lower bound
        If UpperBound < LowerBound Then

            MessageBox.Show("Upper Bound cannot be less than Lower Bound", "Invalid Bounds", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

'declare an array that will store the prime numbers gotten





        For i = LowerBound To UpperBound

        If IsPrime(N) = True Then
        primeTextBox.Text = N

        End If


   Next




    End Sub

    Private Function IsPrime(ByVal number As Long) As Boolean

    If number = 2 Then
       Return True

    Else

        For i = 1 To number \ 2
            If number Mod i = 0 Then
                Return False
            End If
         Next
    End If

Return True
End Function
End Class

I know i need to get the statment that counts from 2 to the square root of the number, and use the mod op in the body of the for next loop. Ive done something similar to that but i think im missing the square root and someething else in my function, or is in my main code?

Line 48 in your function IsPrime was wrong For i = 1 To number \ 2 see this :

Private Function IsPrime(ByVal number As Long) As Boolean
        Dim i As Integer
        If number = 1 Then
            Return False
        ElseIf number = 2 Then
            Return True
        ElseIf number > 2 Then
            For i = 2 To number - 2
                If number Mod i = 0 Then
                    Return False
                End If
            Next i
        End If
        Return True
    End Function

When you cheking prime number, line 28 & 29 is wrong.

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
    primeTextBox.Text = primeTextBox.Text & " " & i
Next

Hope it help

Edited 5 Years Ago by Jx_Man: n/a

Line 48 in your function IsPrime was wrong For i = 1 To number \ 2 see this :

Private Function IsPrime(ByVal number As Long) As Boolean
        Dim i As Integer
        If number = 1 Then
            Return False
        ElseIf number = 2 Then
            Return True
        ElseIf number > 2 Then
            For i = 2 To number - 2
                If number Mod i = 0 Then
                    Return False
                End If
            Next i
        End If
        Return True
    End Function

When you cheking prime number, line 28 & 29 is wrong.

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
    primeTextBox.Text = primeTextBox.Text & " " & i
Next

Hope it help

Thanks man. I really appreciate it.:) i would like to know how to list these numbers in a row.
Ex:1
1
1.. and so on. thanks again.

Edited 5 Years Ago by burntout: fogot a few words

i would like to know how to list these numbers in a row.

you mean to make the results in textbox like a row?
ex :
2
3
5
7

Set textbox properties, Multiline = True
change this following part of your code :

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
      primeTextBox.Text = primeTextBox.Text & VbCrlf & i  ' Change space (" ") with Enter (VbCrlf)
    End If
Next

Edited 5 Years Ago by Jx_Man: n/a

This question has already been answered. Start a new discussion instead.