Need some help with VB (prime numbers and factors)

Status
Not open for further replies.

pinnacle2009

Distinguished
Feb 15, 2010
107
0
18,680
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
Feb 15, 2010
107
0
18,680
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
May 21, 2008
1,290
0
19,310
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.
 
Status
Not open for further replies.