Sum of All Even Numbers in VB6

In this article, we will create a program that can compute the sum of all even numbers using Visual Basic 6.0. We will write a Visual Basic program that reads an integer value and displays the sum of all even integers starting from 2 and input value, inclusive. It will display an error message if the input value is less than 2. 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 only one Button named Command1 and labeled it as "Compute". Add also Two TextBoxes named Text1 for inputting the number and Text2 for displaying the output. You must design your interface like this: design 3. Now put this code for your code module. This code is for Command1_Click:
  1. Private Sub Command1_Click()
  2. Dim sum As Double
  3. If Val(Me.Text1.Text) < 2 Then
  4. MsgBox "Invalid input!", vbCritical
  5. Else
  6. For i = 2 To Val(Me.Text1.Text) Step 2
  7. sum = sum + i
  8. Next i
  9. Text2.Text = "Sum of all even numbers from 2 to " & Me.Text1.Text & ": " & sum
  10. End If
  11. End Sub

Explanation:

We have filter first the value of our inputted number in text1 that if we will input a number below 2, it will prompt the user "Invalid input!". Otherwise, we have created a For Next loop which starts at 2 as our starting number up to the value inputted in Text1 and will Step by 2 as we will have an even number only. Inside the loop, we make the sum variable holds the value of adding all the values of variable i as it was looped. Then the sum of all even numbers from 2 up to the inputted value in text1 will be displayed in Text2.

Output:

output 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