VB.NET Code Snippets: Math, Logic, and Number Operations
VB.NET Code Snippets
Factorial Calculation
VB.NET code to calculate the factorial of a number:
Public Class Form1
Dim no As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
no = 1
For x = 1 To Val(TextBox1.Text)
no = no * x
Next
TextBox2.Text = no
End Sub
End Class
Totalizer Counter
VB.NET code for a totalizer counter:
Public Class Form1
Dim c As Integer
Dim t As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
c = c + 1
TextBox1.Text = c
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
t = t + Val(TextBox3.Text)
TextBox3.Clear()
TextBox3.Focus()
TextBox2.Text = t
End Sub
End Class
Grade Input and Average Calculation
VB.NET code to input grades and calculate the average:
Public Class Form1
Dim x As Integer, numero As Integer, suma As Integer, acu As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = 1
Do While x = 1
numero = InputBox("ingrese una nota", "notas")
suma = suma + numero
acu = acu + 1
x = InputBox("desea ingresar otra nota si=1 / no = 2 \"ingreso\"")
Loop
TextBox1.Text = suma / acu
End Sub
End Class
Finding the Maximum and Minimum Values
VB.NET code to find the maximum and minimum values from a list:
Public Class Form1
Dim n As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(Val(TextBox1.Text))
TextBox1.Clear()
TextBox1.Focus()
n = n + 1
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim contador As Integer
Dim mayor As Integer
contador = 0
mayor = ListBox1.Items(0)
Do While contador < n
contador = contador + 1
If ListBox1.Items(contador) > mayor Then
mayor = ListBox1.Items(contador)
End If
Loop
TextBox2.Text = mayor
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim contador As Integer
Dim menor As Integer
contador = 0
menor = ListBox1.Items(0)
Do While contador < n
contador = contador + 1
If ListBox1.Items(contador) < menor Then
menor = ListBox1.Items(contador)
End If
Loop
TextBox3.Text = menor
End Sub
End Class
Exponent Calculation
VB.NET code to calculate the exponent:
Public Class Form1
Dim n As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
n = 1
For x = 1 To Val(TextBox2.Text)
n = n * Val(TextBox1.Text)
Next
TextBox3.Text = n
End Sub
End Class
Multiplication and Division
VB.NET code for multiplication and division operations:
Public Class Form1
Dim no As Integer, resultado As Integer, no2 As Integer, divisor As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
resultado = 0
no = Val(TextBox1.Text)
no2 = Val(TextBox2.Text)
For x As Integer = 1 To no2
resultado = resultado + no
Next
TextBox3.Text = resultado
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
no = Val(TextBox1.Text)
no2 = Val(TextBox2.Text)
For x = 1 To no
divisor = no - no2
no = divisor
If divisor = 0 Then
TextBox3.Text = x
End If
Next
End Sub
End Class
Number of Divisors
VB.NET code to find the number of divisors:
Public Class Form1
Dim no As Integer
Dim t As Integer
Dim x As Integer
Dim no2 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
no = Val(TextBox1.Text)
Do While x < no
x = x + 1
t = no Mod x
If t = 0 Then
no2 = no2 + 1
TextBox2.Text = no2
End If
Loop
End Sub
End Class
Even and Odd Numbers
VB.NET code to display even and odd numbers:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For x = Val(TextBox1.Text) To Val(TextBox2.Text) Step +2
ListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For x = Val(TextBox1.Text) To Val(TextBox2.Text) Step +2
ListBox1.Items.Add(x)
Next
End Sub
End Class