VB Prog
VB Prog
The Interface
The Code
Private Sub Command1_Click()
Dim AB, AC, BC As Single
AB = Val(Txt_AB.Text)
AC = Val(Txt_AC.Text)
BC = Val(Txt_BC.Text)
If AB <> 0 And AC <> 0 Then
BC = Sqr(AB ^ 2 + AC ^ 2)
Txt_BC.Text = Round(BC, 2)
ElseIf AB <> 0 And BC <> 0 Then
AC = Sqr(BC ^ 2 - AB ^ 2)
Txt_AC.Text = Round(AC, 2)
ElseIf AC <> 0 And BC <> 0 Then
AB = Sqr(BC ^ 2 - AC ^ 2)
Txt_AB.Text = Round(AB, 2)
End If
End Sub
Quadratic Equation Solver
The Interface
The Code
Private Sub Form_Load()
Dim a, b, c, det As Integer
Dim root1, root2 As Single
Dim numroot As Integer
End Sub
Private Sub new_Click()
' To set all values to zero
Coeff_a.Text = ""
Coeff_b.Text = ""
Coeff_c.Text = ""
Answers.Caption = ""
txt_root1.Visible = False
txt_root2.Visible = False
txt_root1.Text = ""
txt_root2.Text = ""
Lbl_and.Visible = False
Lbl_numroot.Caption = ""
End Sub
This program can test whether a number entered by the user is a prime number or not. A
prime number is a number that cannot be divided by other numbers other than by itself.
Examples are 2, 3, 5, 7, 11, 13,17,19, 23,29, 31, 37 and more.
In this program, you can use the Select Case ......End Select statement to determine
whether a number entered by a user is a prime number or not. For case 1, all numbers that
are less than 2 are prime numbers. In Case 2, if the number is 2, it is a prime number. In the
last case, if the number N is more than 2, we need to divide this number by all the numbers
from 3,4,5,6,........up to N-1, if it can be divided by any of these numbers, it is not a prime
number, otherwise it is a prime number. To control the program flow, we can use the
Do......Loop While statement . Besides, we need to tag="Not Prime' to identify the number
that is not prime, so that when the routine exits the loop, the label will display the correct
answer.
The Interface
The Code
Private Sub Command1_Click()
Dim N, D As Single
Dim tag As String
N = Val(TxtNumber.Text)
Select Case N
Case Is < 2
Lbl_Answer.Caption = "It is not a prime number"
Case Is = 2
Lbl_Answer.Caption = "It is a prime number"
Case Is > 2
D=2
Do
If N / D = Int(N / D) Then
Lbl_Answer.Caption = "It is not a prime number"
tag = "Not Prime"
Exit Do
End If
D=D+1
Loop While D <= N - 1
If tag <> "Not Prime" Then
Lbl_Answer.Caption = "It is a prime number"
End If
End Select
End Sub
Geometric Progression
This is a Visual Basic program that generates a geometric progression and displays the
results in a list box. Geometric progression is a sequence of numbers where each
subsequent number is found by multiplying the previous number by a fixed number which is
called common ratio. The common ratio can be negative, an integer, a fraction and any
number but it must not be a zero.
The general formula to find the nth term of the geometric progression is arn-1 , where a is
the first number and r is the common ratio.
In visual basic, we employs the Do.... Loop Until statement to generate the numbers in a
geometric progression.In this program, we need to insert three text boxes for the user to
enter the first number, the common ratio and the number of terms. We also need to insert a
list box to list the numbers. Besides that, a command button is need for the user to generate
the numbers in the geometric progression.
To add the numbers to the list box, we use the AddItem method. The syntax
isList1.AddItem n, where n can be any variable.
The Interface
The Code
Private Sub cmd_compute_Click()
Dim x, n, num As Integer
Dim r As Single
x = Txt_FirstNum.Text
r = Txt_CR
num = Txt_Terms.Text
List1.AddItem "n" & vbTab & "x"
List1.AddItem "___________"
n=1
Do
x=x*r
List1.AddItem n & vbTab & x
n=n+1
Loop Until n = num + 1
End Sub
Factors Finder
This is a program that can find factors of a number entered by the user and display them in
a list box. We use the simple logic that a number is divisible by all its factors. However, you
need to tell Visual Basic how to identify factors from non factors. In order to acheive this ,
we can make use of the fact that the remainder after a number is divided by its factor is
zero. In Visual Basic, you can use the MOD operator, which compute the value of the
remainder after a number is divided by an integer. The format is N Mod x.
With these logics in mind, we can use a For....Next Loop to evaluate all the remainders for
all the divisors of N smaller than N. If the remainder is zero, then the divisor x is added to
the list box. In this way, we are able to compute all the factors.
The Interface
The code
Private Sub Command1_Click()
Dim N, x As Integer
N = Val(Text1.Text)
For x = 2 To N - 1
If N Mod x = 0 Then
List1.AddItem (x)
End If
Next
List1.AddItem (N)
End Sub