Finding Factors Program in VB6

Factor is a a number or quantity that when multiplied with another produces a given number or expression. In this article, we will create a program that an find factors of a number input by the user and display them in a list box. Now, let's start this tutorial! 1.Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open. 2. Next, add two Buttons named Command1 labeled it as "Find Factor" and Command1 labeled as "Clear". Add one textbox named Text1 for inputting the desired number and insert also a listview named List1 for displaying the list of numbers divisible by the inputted number in the textbox. You must design your interface like this: design 3. Now put this code for your code module.
  1. Private Sub Command1_Click()
  2. Dim strInt, x As Integer
  3. strInt = Val(Text1.Text)
  4. For x = 2 To strInt - 1
  5. If strInt Mod x = 0 Then
  6. List1.AddItem (x)
  7. End If
  8. Next
  9. List1.AddItem (strInt)
  10. End Sub
  11.  
  12. Private Sub Command2_Click()
  13. Text1.Text = ""
  14. List1.Clear
  15. End Sub

Explanation:

We only used the simple logic in math that a number is divisible by all its factors. However, we need also to solve how to identify factors from non factors. In order to do this , we can make use of the fact that the remainder after a number is divided by its factor is zero. In Visual Basic, we can use the MOD operator, which compute the value of the remainder after a number is divided by an integer. The format is strInt Mod x. strInt variable here holds the value of our Text1 as input. Then all the factors of the inputted number holds the x variable and will display in the Listview. The command2 button was only used for clearing the text inside Text1 and List1.

Output:

Download the source code and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment