Pages

Monday, July 21, 2014

Prime numbers in Visual Basic 6.0

Prime numbers as other classifications of natural numbers were classified by Pythagoras and his sect, whose members were called Pythagoreans-.

This program will result in the prime numbers from a value to another that we specify.

For the form we'll need 2 textbox (txtFrom and txtTo). Two labels, one listbox (lstResult) and 1 commandbutton (cmdCalculate). As shown in the right image.

In the calculate button insert the following code:





Option Explicit

Dim d, h, v, a, b, c As Integer


Private Sub cmdCalculate_Click()

If Val(txtFrom.Text) < Val(txtTo.Text) Then
lstResult.Clear

d = Val(txtFrom.Text)
h = Val(txtTo.Text)
v = d - 1

Do While v < h
v = v + 1

'Identificar si el numero es primo
a = 0
b = 1


Do While b <= v
c = v Mod b

If c = 0 Then
a = a + 1
b = b + 1

Else
b = b + 1
End If

Loop

If a = 2 Or v = 1 Then

lstResult.AddItem (v)
End If
Loop

Else
MsgBox ("The value of From must be greater tha the value of To")
End If

txtFrom.Text = ""
txtTo.Text = ""
txtFrom.SetFocus

End Sub



Image: Rest up, little Pikachu! by xThunderbolt