' IsPrime Example ' Input a number. Output whether the number is prime or not. ' Recall that a number is prime if its only divisors are 1 and ' the number itself. Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents btnCheck As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lblOutput As System.Windows.Forms.Label Friend WithEvents txtInt As System.Windows.Forms.TextBox Private Sub InitializeComponent() Me.txtInt = New System.Windows.Forms.TextBox Me.btnCheck = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.lblOutput = New System.Windows.Forms.Label Me.SuspendLayout() ' 'txtInt ' Me.txtInt.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtInt.Location = New System.Drawing.Point(24, 16) Me.txtInt.Name = "txtInt" Me.txtInt.Size = New System.Drawing.Size(144, 29) Me.txtInt.TabIndex = 0 Me.txtInt.Text = "" ' 'btnCheck ' Me.btnCheck.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnCheck.Location = New System.Drawing.Point(184, 16) Me.btnCheck.Name = "btnCheck" Me.btnCheck.Size = New System.Drawing.Size(176, 32) Me.btnCheck.TabIndex = 1 Me.btnCheck.Text = "Check if Prime" ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(64, 56) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(72, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "Integer" ' 'lblOutput ' Me.lblOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblOutput.Location = New System.Drawing.Point(32, 88) Me.lblOutput.Name = "lblOutput" Me.lblOutput.Size = New System.Drawing.Size(264, 32) Me.lblOutput.TabIndex = 3 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(424, 134) Me.Controls.Add(Me.lblOutput) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.btnCheck) Me.Controls.Add(Me.txtInt) Me.Name = "Form1" Me.Text = "IsPrime Example" Me.ResumeLayout(False) End Sub #End Region Private Sub btnCheck_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCheck.Click Dim n As Integer If Not IsNumeric(txtInt.Text) Then lblOutput.Text = "Not numeric." Exit Sub End If If txtInt.Text.IndexOf(".") > -1 Then lblOutput.Text = "Decimal point not allowed." Exit Sub Else n = txtInt.Text End If ' In case of negative input, convert to positive. n = Math.Abs(n) If IsPrime(n) Then lblOutput.Text = "Is prime." Else lblOutput.Text = "Is not a prime." End If End Sub Private Function IsPrime(ByVal x As Integer) As Boolean Dim factor As Integer ' Zero and one are not considered prime. If x = 0 Or x = 1 Then Return False End If ' Check possible factors of x. If a factor is found, ' x is not prime. For factor = 2 To x - 1 If x Mod factor = 0 Then Return False End If Next Return True End Function End Class