PDA

Click to See Complete Forum and Search --> : Prime Nos


lavureddygudellyjn
Dec 14th, 1999, 05:46 PM
how to findout prime nos between 1 to 10

Dec 14th, 1999, 07:00 PM
Dim arr(0 To 9) As Integer 'Array to store the Prime Numbers.

Dim intArrCount As Integer 'Stores the total number of items inserted into the array.

Dim i As Integer, j As Integer 'Loop Counters.

Dim blnDivisible As Boolean 'A flag. If a number is divisible, it is set to True.

'Initialize.
intArrCount = 0 'Total Prime Numbers within array set to 0.
blnDivisible = False 'Default.
i = 2 'Number check from 2.

While i <= 10
'Check if number i is divisible by the each number within the array.

For j = 1 To intArrCount
If i Mod arr(j) = 0 Then
'The number returns a remainder. Set the Flag to True.

blnDivisible = True
Exit For
End If
Next j

If (blnDivisible = False) Then
'Else store the number in the array.
intArrCount = intArrCount + 1
arr(intArrCount) = i
End If
blnDivisible = False 'Reset Flag.
i = i + 1 'Next number.
Wend

'1 is always a Prime Number. Put it at the first position of the array.
arr(0) = 1

'Dispay the results of the array.
Dim strPrimeNos As String
For i = 0 To intArrCount
strPrimeNos = strPrimeNos & ", " & arr(i)
Next i

'Remove the extra ", " at the beginning of the string.
strPrimeNos = Mid(strPrimeNos, 3)

MsgBox "The Prime Numbers are " & strPrimeNos