Friday, April 7, 2017

Sum of Even Numbers in Visual Basic NET

Hi there in this article I would like to share with you a sample program that will ask the user to give a number and then our program will compute the total sum of all the even numbers in the given number value by our user using Visual Basic NET. The code is very simple and easy to understand. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.
 
 


 
Sample Program Output

Program Listing

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sum As Integer
        If Val(TextBox1.Text) < 2 Then
            MsgBox("Invalid value !!! Try Again", vbCritical)
        Else
            For a = 2 To Val(TextBox1.Text) Step 2
                sum = sum + a
            Next a
            Label2.Text = "The sum of all even numbers from 1 to " & TextBox1.Text & ": " & sum & "."
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        End
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        Label2.Text = ""
        TextBox1.Focus()
    End Sub
End Class

No comments:

Post a Comment