Visual Basic 6.0 Practicals
Visual Basic 6.0 Practicals
01190301912
Q20. Write a program to solve a general quadratic equation ax
2
+ bx + c=0.
Solution : Private Sub Command1_Click()
Dim a, b, c As Integer
Dim r1, r2, d As Double
a = Val(txta.Text)
b = Val(txtb.Text)
c = Val(txtc.Text)
d = (b * b) - (4 * (a * c))
If (d > 0) Then
r1 = (-b + Sqr(d)) / (2 * a)
r2 = (-b - Sqr(d)) / (2 * a)
MsgBox ("r1 = " & r1 & "and r2 = " & r2)
ElseIf d = 0 Then
r1 = (-b) / (2 * a)
r2 = r1
MsgBox ("Roots are equal" & "r1 =" & r1 & "r2 = " & r2)
Else
MsgBox ("Roots are imaginary")
End If
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q28. Create a solution to display the following series:-
a) 1, 3, 5, 7.15
Solution :- Dim num As Double
Dim i As Double
Private Sub Command1_Click()
For i = 1 To 15 Step 2
List1.AddItem (i)
Next i
End Sub
b) 0.2, 0.4, 0.6..2.0
Solution :- Private Sub Command2_Click()
For i = 0.2 To 2# Step 0.2
List2.AddItem (i)
Next i
End Sub
c) 15, 13, 11, 9, 7.1
Solution :- Private Sub Command3_Click()
For i = 15 To 1 Step -2
List3.AddItem (i)
Next i
End Sub
VB PRACTICAL FILE
01190301912
d) 10, 5, 0 .5,-10
Solution :- Private Sub Command4_Click()
For i = 10 To -10 Step -5
List4.AddItem (i)
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
e) 1 + 2 + 3 +...............+n
Solution :- Private Sub Command5_Click()
num = InputBox("Enter the limit" )
For i = 1 To num Step 1
List5.AddItem (i)
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
f) 1+3+5+n
Solution :- Private Sub Command6_Click()
num = InputBox("Enter the limit")
For i = 1 To num Step 2
List6.AddItem (i)
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
Q30. Write a program to display the square and cube of the numbers from
1 to 100 using
i) For-next loop
ii) While-wend loop
iii) Do while loop
Number Square Cube
---------- ---------- ---------
Solution :-
1.For loop button
Private Sub Command1_Click()
i = 1
For i = 1 To 100
List1.AddItem (i)
List2.AddItem (i * i)
List3.AddItem (i * i * i)
Next i
End Sub
2.Do while loop button
Private Sub Command2_Click()
i = 1
Do While (i <= 100)
List1.AddItem (i)
List2.AddItem (i * i)
List3.AddItem (i * i * i)
i = i + 1
Loop
End Sub
VB PRACTICAL FILE
01190301912
3.While loop button
Private Sub Command3_Click()
i = 1
While (i <= 100)
List1.AddItem (i)
List2.AddItem (i * i)
List3.AddItem (i * i * i)
i = i + 1
Wend
End Sub
4.Clear button
Private Sub Command4_Click()
List1.Clear
List2.Clear
List3.Clear
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q31. Create an solution to print the following patterns:-
Solution :-
a) *
**
***
****
Private Sub Command1_Click()
Dim en As Integer
en = Val(txten.Text)
For i = 1 To en Step 1
For j = 1 To i Step 1
Print * ;
Next j
Print VBendline
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
b) 1
1 2
1 2 3
1 2 3 4 5
Private Sub Command1_Click()
Dim en As Integer
en = Val(txten.Text)
For i = 1 To en Step 1
For j = 1 To i Step 1
Print j;
Next j
Print VBendline
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
c) 5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Private Sub Command1_Click()
Dim en As Integer
en = Val(txten.Text)
For i = en To 1 Step -1
For j = i To 1 Step -1
Print j;
Next j
Print VBendline
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
d) 1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Private Sub Command1_Click()
Dim en As Integer
en = Val(txten.Text)
For i = en To 1 Step -1
For j = 1 To i
Print j;
Next j
Print VBendline
Next i
End Sub
Output:-
VB PRACTICAL FILE
01190301912
Q34. Create an application to find out factorial of a number.
Solution :-
Private Sub Command1_Click()
Dim num, fact As Double
fact = 1
num = Val(txten.Text)
For i = 1 To num
fact = fact * i
Next i
MsgBox ("Factorial of number is " & fact)
End Sub
Output:-
VB PRACTICAL FILE
01190301912
Q36. Create an application to check whether then entered number is
Armstrong or not.
Solution :-
Private Sub Command1_Click()
Dim num, n, r, s As Integer
s = 0
num = Val(txten.Text)
n = num
While (n <> 0)
r = n Mod 10
s = s + (r * r * r)
n = n \ 10
Wend
If s = num Then
MsgBox ("Number is Armstrong Number")
Else
MsgBox ("Number is not Armstrong Number")
End If
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q37. Create an application to print the Fibonacci Series within a given
range.
Solution :-
Private Sub Command1_Click()
Dim a, b, lim As Integer
lim = Val(txtlim.Text)
a = 0
b = 1
List1.AddItem (a)
List1.AddItem (b)
For i = 1 To lim - 2
s = a + b
List1.AddItem (s)
a = b
b = s
Next i
End Sub
Private Sub Command2_Click()
List1.Clear
txtlim.Text = " "
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q43. Write a program to add and remove 5 command buttons using
Control Array.
Solution :-
Static Array
Step 1:- Drag and drop a Command button on the form.
Step 2:- Put the index value of button to 0.
VB PRACTICAL FILE
01190301912
Step 3:- Copy - Paste this button on the form. Index value of the button will be automatically
changed (increased).
Step 4:- Copy Paste three more buttons. Index value is increased accordingly.
VB PRACTICAL FILE
01190301912
Step 5:- Write a code in one button. Here:- a message to display Hello.
Step 6:- By clicking on any button will show the same output. All button when click will
show Hello message.
VB PRACTICAL FILE
01190301912
Dynamic Static
Step 1:- Drag and drop a command button and put the value of Index to 0.
Step 2:- double click on form(anywhere); source code will open , write the following code to
create dynamic array i.e., when you execute the form automatically buttons will be formed.
Private Sub Form_Load()
Dim a As Integer
button(0).Caption = 0
For i = 1 To 5
Load button(i)
button(i).Top = button(i - 1).Top + 1.2 * button(0).Height
button(i).Caption = i
button(i).Visible = True
Next i
For i = 2 To 5
Unload button(i)
Next i
End Sub
Step 3:- Write the following code in the button to show the index value in the text box.
Private Sub button_Click(Index As Integer)
Text1.Text = Index
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q33. Create an application to check whether an entered number is Prime
or not.
Solution :-
Private Sub Command1_Click()
prime
End Sub
Public Sub prime()
Dim n, flag As Integer
n = InputBox("Enter Number")
flag = 0
For i = 2 To x - 1
If (x Mod i = 0) Then
flag = 1
Exit For
End If
Next i
If flag = 0 Then
MsgBox ("Number is Prime")
Else
MsgBox ("Number is not Prime")
End If
End Sub
VB PRACTICAL FILE
01190301912
Output:-
VB PRACTICAL FILE
01190301912
Q44. Write a program to check whether the entered number is Prime or
not with Procedure.
Solution :-
Private Sub Command1_Click()
Dim n As Integer
n = InputBox("Enter Number")
prime n
End Sub
Public Sub prime(ByVal x As Integer)
Dim flag As Integer
flag = 0
For i = 2 To x - 1
If (x Mod i = 0) Then
flag = 1
Exit For
End If
Next i
If flag = 0 Then
MsgBox ("Number is Prime")
Else
MsgBox ("Number is not Prime")
End If
End Sub
VB PRACTICAL FILE
01190301912
Output:-