Sign in with
Sign up | Sign in
Your question

Need some help with VB (prime numbers and factors)

Last response: in Applications
Share
September 27, 2010 10:31:12 PM

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:

  1. Dim num As Integer
  2. Dim factor As Integer
  3. factor = 1
  4. num = TextBox1.Text
  5. Do While num >= factor
  6. If num Mod factor = 0 Then
  7. RichTextBox1.AppendText(vbCrLf & factor)
  8. End If
  9. factor = factor + 1
  10. Loop


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

More about : prime numbers factors

September 28, 2010 12:33:09 PM

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

  1. Dim num As Integer
  2. Dim i As Integer
  3. Dim counter As Integer
  4. num = TextBox1.Text
  5. counter = 0
  6. i = 1
  7. Do While i <= num
  8. If num Mod i = 0 Then
  9. counter = counter + 1
  10. End If
  11. i = i + 1
  12. Loop
  13. If counter = 2 Then
  14. TextBox2.Text = "This is a prime number!"
  15. Else
  16. TextBox2.Text = "This is NOT a Prime number!"
  17. End If


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

Could someone explain?
m
0
l
a b L Programming
September 28, 2010 2:00:35 PM

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.
m
0
l
September 29, 2010 12:15:15 AM

got it now. we actually just did stuff with it in class today.
m
0
l
!