Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual basic question I have this code to find whether it is a prime number or n

ID: 3760430 • Letter: V

Question

Visual basic question

I have this code to find whether it is a prime number or not and it is working

but i need to implement this code using function like

Function


end Function

Please help me how to use function finding whether it is prime or not
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim k As Boolean
        Dim intNum As Integer
        Dim intNum2 As Integer
        If Integer.TryParse(TextBox1.Text, intNum) And intNum > 0 Then
            intNum = TextBox1.Text
            k = True
            For intNum2 = 2 To (intNum - 1)
                If intNum Mod intNum2 = 0 Then
                    k = False
                    Exit For

                End If
            Next intNum2
            If k Then
                lblMessage.Text = "This number is prime"
            Else
                lblMessage.Text = "This number is not a prime"

            End If
        Else
            MessageBox.Show("Invalid Input , Please Enter a positive integer")
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox1.Focus()
        lblMessage.Text = String.Empty
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class

Explanation / Answer

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim k As Boolean
        Dim intNum As Integer
        Dim intNum2 As Integer
        If Integer.TryParse(TextBox1.Text, intNum) And intNum > 0 Then
            intNum = TextBox1.Text
            k = True
            For intNum2 = 2 To (intNum - 1)
                If intNum Mod intNum2 = 0 Then
                    k = False
                    Exit For
                End If
            Next intNum2
            If k Then
                lblMessage.Text = "This number is prime"
            Else
                lblMessage.Text = "This number is not a prime"
            End If
        Else
            MessageBox.Show("Invalid Input , Please Enter a positive integer")
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox1.Focus()
        lblMessage.Text = String.Empty
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class

This Code is working, I mean Showing no error for me