Question Help with a very basic prime numbers array program

meme12

New member
Joined
Apr 20, 2011
Messages
2
Programming Experience
Beginner
First off, I'm not sure if this is the right forum, please move it if it isn't. I am only just beginning with visual basic and I am trying to write a basic program to calculate the prime numbers from 1 to 100. This is what I have so far:

VB.NET:
        Dim flag As Integer
        Dim ar(101) As Integer


        For i = 1 To 100
            flag = 0
            For j = 2 To ar(i) / 2   
                If ar(i) Mod j = 0 Then
                    flag = 1
                    Exit For   
                End If
            Next
            If flag = 0 Then
                ListBox1.Items.Add((i) & " is Prime")
            End If
        Next

Can someone please help me make this work. Thanks.
 
Since the number of items is not finite you should be using a List(Of Integer), not an array. Whenever you have identified a prime you just add it to the list; list.Add(N).
 
Back
Top