Table of Contents
Tutorial: How to Determine Even or Odd Number in VB.NET with Source Code
About How to Determine Even or Odd Number in VB.NET Tutorial
This is a tutorial on how to determine even or odd number in VB.NET. Even Numbers are any integer that can be divided exactly by 2. The last digit will be 0, 2, 4, 6, or 8. If it is not an even number, it is called an odd number. The last digit will be 1, 3, 5, 7, or 9.
In this tutorial, we will create a program that can determine a number whether odd or even.
Now, let’s start this tutorial!
- Let’s start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
- Next, add only one Button named Button1 and labeled it as “Determine if Odd or Even Number“. Insert also Textbox and named it as Textbox1 for inputting the desired number. You must design your interface like this:
- Now, Copy and Paste the code below for your code module. The provided code is for the functionality of the “Button1_Click”.
Button Click Function Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Mynumber As Integer Dim isEven As Boolean Mynumber = Val(TextBox1.Text) If Mynumber Mod 2 = 0 Then isEven = True MsgBox("The number " & " " & Mynumber & " is an even number") Else MsgBox(Mynumber & " " & "is an Odd number") End If End Sub
We initialized variable “Mynumber” as Integer to hold the value inputted in “textbox1”. The main reason to get the even number is to get their value as divisible by 2 which is equal to zero. So that is why we used the Mod function. Mod Operator divides two numbers and returns only the remainder. The number inputted in textbox1 was divided by 2 and if the remainder is zero then we considered it as an even number. Otherwise, it is an odd number in our If-Else statement.