Function IsPrimeTarea Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function IsPrimeTarea 21


Objetive:

Create a function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not:

if (isPrime(127)) ...


Code:

Imports System
Public Class exercise111
    Public Shared Function IsPrime(ByVal n As Integer) As Boolean
        For i As Integer = 2 To n - 1
            If n Mod i = 0 Then Return False
        Next

        Return True
    End Function

    Public Shared Sub Main()
        If IsPrime(127) Then
            Console.WriteLine("127 is a prime number")
        Else
            Console.WriteLine("127 is NOT a prime number")
        End If

        If IsPrime(1000003) Then
            Console.WriteLine("1000003 is a prime number")
        Else
            Console.WriteLine("1000003 is NOT a prime number")
        End If
    End Sub
End Class