Sum of All Odd Numbers in VB6

I already introduced a program that can calculate the sum of all even numbers in visual basic 6.0 and now we will create another but this time it will be an odd numbers summation. We will write a Visual Basic program that reads an integer value and displays the sum of all odd integers starting from 1 and input value, inclusive. It will display an error message if the input value is less than 1. 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) < 0 Then
  4. MsgBox "Invalid input!", vbCritical
  5. Else
  6. For i = 1 To Val(Me.Text1.Text) Step 2
  7. sum = sum + i
  8. Next i
  9. Text2.Text = "Sum of all odd numbers from 1 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 1 as our starting number up because odd number will start at 1 to the value inputted in Text1 and will Step by 2 as we will have an odd 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 odd numbers from 1 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