How to use CheckIfPrime

I need to use an element CheckIfPrime

in my code, but having some problem, may I know how I should deal with it? Do I need to make an expression?

For number As Integer = 1 To 30
    If CheckIfPrime(number) = True Then
        sb.Append(number.ToString & " ")
    End If
Next

      

I am asking for advice on how I can do this in Visual Basic.

+3


source to share


1 answer


VB.net

has no method CheckIfPrime

, so in order to handle it in your task, we need to create a method to check the number if it is simple or not.

   Public Function CheckIfPrime(number As Integer) As Boolean
        For i As Integer = 2 To number - 1
            If number Mod i = 0 Then
                Return False
            End If
        Next
        Return True
    End Function

      

Using



   For number As Integer = 1 To 30
      If CheckIfPrime(number) = True Then
       'Console.WriteLine(number.ToString & " ")
       sb.Append(number.ToString & " ")
      End If
   Next

      

Source

+2


source







All Articles