3

I need to use the CheckIfPrime item in my code, but having some issue, may i know how should I deal with it? Do I need to make declaration?

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

Please advice on how can I do that in Visual Basic.

spajce
  • 7,044
  • 5
  • 29
  • 44
LukeLee
  • 49
  • 5

1 Answers1

2

The VB.net has no CheckIfPrime method, so to deal with it in your task we need to create a method to check the number if is prime 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

Usage

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

Source

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44