'Author: Eric Heim 'Date Created: 3/30/2011 'Course: CS0007 'Description: This program takes a positive whole number from the user and displays that ' numbers prime factorization. If the user does not enter a positive whole number ' the program displays a message box telling them so. Option Strict On Option Explicit On Option Infer Off Public Class frmMain Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click 'If the number is a positive whole number If IsPositiveWhole(txtNumber.Text) Then 'Put the string of prime factors into the factors text box txtFactors.Text = FindPrimeFactors(CInt(txtNumber.Text)) Else MessageBox.Show("You did not enter a positive, whole number.", "Invalid Input") End If End Sub 'Name: IsPositiveWhole 'Parameters: stringNumber - string to check if its a positive whole number 'Returns: Boolean - true if it is a positive, whole number, false if it isn't 'Description: Checks to see if the string parameter is a positive whole number Function IsPositiveWhole(ByVal stringNumber As String) As Boolean If IsNumeric(stringNumber) Then If CDbl(stringNumber) = CDbl(Int(stringNumber)) And CInt(stringNumber) >= 0 Then Return True End If End If Return False End Function 'Name: FindPrimeFactors 'Parameters: inNumber - integer to find its prime factors, MUST BE POSITIVE 'Returns: String of inNumbers prime factors, separated by spaces 'Description: Finds the parameters prime factors and returns them as a string, each ' factor separated by a space Function FindPrimeFactors(ByVal inNumber As Integer) As String Dim factors As String = "", currentFactor As Integer = 2 'The prime factorization of 1 is 1 If inNumber = 1 Then factors &= "1 " Else 'Loop while there are still factors left Do While inNumber > 1 'If the current number can be divided by the current factor If inNumber Mod currentFactor = 0 Then 'Add the current factor to the string and divide the current number by the 'current factor factors &= CStr(currentFactor & " ") inNumber = inNumber \ currentFactor Else 'Increment the current factor currentFactor += 1 End If Loop End If 'Return the list of factors Return factors End Function End Class