Results 1 to 2 of 2

Thread: Prime Nos

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 1999
    Location
    secunderabad,A.P,INDIA
    Posts
    4

    Post

    how to findout prime nos between 1 to 10

  2. #2
    Guest

    Post

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width