i have to create application that ask user to input Lower bound and upper bound (both intger) greater than 1 and determins all of the prime numbers within the specified bounds, inclusive. and write function procedure prime that returns True if a number is prime. False otherwise. i also should use error provider.


i wrote these 2.. but both are not working ?? :'(
i don't know where is my mistake
any help plz ?? :idea:

i have to submit my project tomorrow :(

Public Class Form1

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

Dim i As Integer
Dim a As Integer
Dim b As Integer

ListBox1.Items.Add(Prime(Val(TextBox1.Text))


For i = a To b

ListBox1.Items.Add(i)

Next

End Sub
Function Prime(ByVal b As Integer) As Boolean
Dim i As Integer
Dim a As Integer
a = TextBox1.Text
b = TextBox2.Text

For i = a To b - 1
If b Mod a = 0 Then
Prime = False
Exit Function
End If

Next i
Prime = True
ListBox1.Items.Add(i)
Return a
End Function
End Class

Public Class Form1
Dim i As Integer
Dim n As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub btnCalP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalP.Click

a = Val(txtLoBo.Text)
b = Val(txtUpBo.Text)
lstPN.Items.Add(Prime(Val(txtLoBo.Text), Val(txtUpBo.Text)))

For i = a To b
If b Mod 2 <> 0 Then
lstPN.Items.Add(c)
End If
Next

End Sub

Function Prime(ByVal a As Integer, ByVal b As Integer) As Integer


Dim flag As Boolean = True

If a Mod 2 <> 0 Then
For c = 2 To b - 1
If a Mod c = 0 Then
flag = False


Else
lstPN.Items.Add(c)
flag = True
End If
Next
end if

Return flag = True
Return flag = False
End Function

End Class

Recommended Answers

All 5 Replies

first you must know the type and patern of prime number :
Hope the following code is solved your problem :

Private Sub PrimeNumber(ByVal Low As Integer, ByVal Up As Integer)
        Dim i As Integer
        For i = Low To Up
            If (i = 2) Or (i = 3) Or (i = 5) Or (i = 7) Then 
                ListPN.Items.Add(i)
            Else
                If (i Mod 2 <> 0) And (i Mod 3 <> 0) And (i Mod 5 <> 0) And (i Mod 7 <> 0) Then
                    ListPN.Items.Add(i)
                End If
            End If
        Next
 End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListPN.Items.Clear()
        PrimeNumber(txtLower.Text, txtUpper.Text)
End Sub

Hope this helps.
don't forget to give feedback

Thaaaaaaaaaaaaaaaaaaaaaaaaaaaanks 4 ur helping.. :)

it's work now :)

you're welcome :)
don't forget to mark this thread solved

you're welcome :)
don't forget to mark this thread solved

i have problem with the result

if i enter 1 , 199

it give all prime numbers between to thses nums

but it give also 200 which is even num. and it's not between 1 and 199 ??

i have tried it. and i don't get result 200.just between 1-199.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.