# Need some help with VB (prime numbers and factors)

#### pinnacle2009

##### Distinguished
I am trying to get a VB program that will determine whether or not a user-generated number is prime or not. Right now, all I have is this:

Code:
``````Dim num As Integer
Dim factor As Integer
factor = 1
num = TextBox1.Text
Do While num >= factor
If num Mod factor = 0 Then
RichTextBox1.AppendText(vbCrLf & factor)
End If
factor = factor + 1
Loop``````

It's just getting the factors of a number. I cannot get it to tell prime numbers apart, however. Any help?

#### pinnacle2009

##### Distinguished
Okay, after looking and and searching, I now have:

Code:
``````Dim num As Integer
Dim i As Integer
Dim counter As Integer
num = TextBox1.Text
counter = 0
i = 1
Do While i <= num
If num Mod i = 0 Then
counter = counter + 1
End If
i = i + 1
Loop
If counter = 2 Then
TextBox2.Text = "This is a prime number!"
Else
TextBox2.Text = "This is NOT a Prime number!"
End If``````

However, I don't know what the counter does exactly.. (part of this was copied)

Could someone explain?

#### kyeana

##### Distinguished
Here is a hint, the defination of a prime number is a number that can be divided by one and itself. Go through the code line by line and see if you can't understand what it is doing.

#### pinnacle2009

##### Distinguished
got it now. we actually just did stuff with it in class today.

Replies
2
Views
15K
Replies
3
Views
2K
Replies
11
Views
45K
Replies
3
Views
5K
Replies
3
Views
4K