Sei sulla pagina 1di 36

Q1.

Write a VB program to find out twin prime


numbers between 10 to 100

Private Sub Form_click()


Dim p, n, i As Integer
p=1
Print "Twin Prime numbers from 10 to 100 are:"
For n = 10 To 100
For i = 2 To n - 1
If n Mod i = 0 Then
p=0
Exit For
Else
p=1
End If
Next i
If p = 1 Then
Print i
End If
Next n
End sub
Q2. Design a calculator in VB.

Dim op, fn As Integer


Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text + Trim(Str(Index))
End Sub

Private Sub Command2_Click()


Text1.Text = Text1.Text + "."
End Sub

Private Sub Command3_Click()


Text1.Text = ""

End Sub

Private Sub Command4_Click()


op = 1
fn = Text1.Text
Text1.Text = ""

End Sub

Private Sub Command5_Click()


op = 3
fn = Text1.Text
Text1.Text = ""
End Sub

Private Sub Command6_Click()


If op = 1 Then
Text1.Text = Val(fn) + Val(Text1.Text)
ElseIf op = 2 Then
Text1.Text = Val(fn) - Val(Text1.Text)
ElseIf op = 3 Then
Text1.Text = Val(fn) * Val(Text1.Text)
ElseIf op = 4 Then
Text1.Text = Val(fn) / Val(Text1.Text)
End If
End Sub

Private Sub Command7_Click()


op = 2
fn = Text1.Text
Text1.Text = ""

End Sub

Private Sub Command8_Click()


op = 4
fn = Text1.Text
Text1.Text = ""

End Sub
Private Sub Command9_Click()
If Len(Text1.Text) = 0 Then Exit Sub
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
End Sub

Private Sub Form_Load()


For i = 0 To 9
Command1(i).Caption = i
Next i

End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
Q3. Draw a circle which can shink, expand, erase
and exit.

Private Sub Command1_Click()


Shape1.Visible = True
End Sub

Private Sub Command2_Click()


Shape1.Width = Shape1.Width - 10
Shape1.Height = Shape1.Height - 10
End Sub

Private Sub Command3_Click()


Shape1.Width = Shape1.Width + 10
Shape1.Height = Shape1.Height + 10
End Sub

Private Sub Command4_Click()


Shape1.Visible = False
End Sub

Private Sub Command5_Click()


End
End sub
Q4. VB program to find out sum of digits of a given
number till it reduces to single digit. Accept input
through textbox and output through msgbox (using
function).

Dim s As Integer
Public Function display(s)
s=0
n = Val(Text1.Text)
While n > 9
s=0
While n > 0
r = n Mod 10
s=s+r
n = n \ 10
Wend
n=s
Wend
display = s
End Function

Private Sub Command1_Click()


sum1 = display(s)
MsgBox sum1, vbOKOnly, "SUM OF DIGITS"
End Sub
Q5. VB program for Dental payment form.
Dim sum As Integer

Private Sub Command1_Click()


If Check1.Value = 1 Then
sum = sum + 35
End If
If Check2.Value = 1 Then
sum = sum + 150
End If
If Check3.Value = 1 Then
sum = sum + 85
End If
If Check4.Value = 1 Then
sum = sum + 50
End If
If Check5.Value = 1 Then
sum = sum + 800
End If
If Check6.Value = 1 Then
sum = sum + Val(Text2.Text)
End If
Text3.Text = sum
sum = 0
End Sub
KAJAL SAHA
Q6. VB program to place three text box onto the
form at run time. Enter different strings in first
and second textbox. On clicking to command
button, concatenation of two strings should be
displayed in the third text box.

Dim WithEvents text1 As TextBox


Dim WithEvents text2 As TextBox
Dim WithEvents text3 As TextBox
Dim WithEvents cmd As CommandButton

Private Sub cmd_Click()


text3.Text = text1.Text + text2.Text
End Sub

Private Sub Form_Load()


Set cmd = Controls.Add("vb.commandbutton", "cmd")
cmd.Left = 600
cmd.Top = 1000
cmd.Caption = "concat"
cmd.Visible = True
Set text1 = Controls.Add("vb.textbox", "text1")
text1.Left = 100
text1.Top = 100
text1.Visible = True
Set text2 = Controls.Add("vb.textbox", "text2")
text2.Left = 1500
text2.Top = 100
text2.Visible = True

Set text3 = Controls.Add("vb.textbox", "text3")


text3.Left = 3000
text3.Top = 100
text3.Visible = True

End Sub

kajal saha kajalsaha


Q7. VB program to display the reverse of a given
number using function.(accept number through
textbox and display result using message box)

Private Sub Command1_Click()


Dim n As Integer
n = Val(Text1.Text)
Dim a As Integer
Dim r As Integer
While n <> 0
a = n Mod 10
r = r * 10 + a
n = n \ 10
Wend
MsgBox ("Revesre is " & r)
End Sub
Q8. write a vb program to display Fibonacci series
upto a give term

Private Sub Command1_Click()


Dim x, y, i, n As Integer
n = Val(Text1.Text)
x=0
y=1
Print x
Print y
For i = 3 To n
Sum = x + y
Print Sum
x=y
y = Sum
y = Sum
Next i
End Sub
Q9.write a vb program to find transpose of a
matrix
Dim a(), trans(), i, j, r, c As Integer
Private Sub Command1_Click()
r = InputBox("enter rows")
c = InputBox("enter cols")
ReDim a(r, c)
ReDim trans(r, c)
Print "matrices is"
For i = 1 To r
For j = 1 To c
a(i, j) = InputBox("enter")
Print a(i, j); " ";
Next
Print " "
Next
Print "transpose is"
For i = 1 To r
For j = 1 To c
trans(i, j) = a(j, i)
Print trans(i, j); " ";
Next
Print " "
Next
End Sub
Q10. write a vb programme to chck if number
is palindrome or no

Private Sub Command1_Click()


Dim x, a, s, n As Integer
n = Val(Text1.Text)
a=n
s=0
While (n > 0)
x = n Mod 10
s = s * 10 + x
n = n \ 10
If (a = s) Then
Label1.Caption = "palindrome number"
Else
Label1.Caption = "not palindrome number"
End If
Wend
End sub
Q11. wrrite a vb program to convert celcius to
farh
Private Sub Command1_Click()

Dim a, b As Integer
a = Text1.Text
b = (a * 9 / 5) + 32
MsgBox "fahrehneit " & b
End Sub
Q12. VB PROGRAM THAT TAKE DOB FRO USER
AND FIND OUT THE EXACT AGE
Private Sub Command1_Click()
Dim bday As Date, target As Date
Dim numMo As Integer, numYrs As Integer,
numDays As Integer
Dim tmpDate As Date
bday = #5/28/1979#
target = Date
If target <= bday Then
MsgBox "Invalid date"
Exit Sub
End If
numYrs = DateDiff("yyyy", bday, target)
tmpDate = DateAdd("yyyy", numYrs, bday)
If tmpDate > target Then
numYrs = numYrs - 1
tmpDate = DateAdd("yyyy", -1, tmpDate)
End If
numMo = DateDiff("m", tmpDate, target)
tmpDate = DateAdd("m", numMo, tmpDate)
If tmpDate > target Then
numMo = numMo - 1
tmpDate = DateAdd("m", -1, tmpDate)
End If
numDays = DateDiff("d", tmpDate, target)
MsgBox "Years = " & numYrs & vbCrLf & "Months
= " & numMo & vbCrLf & "Days = " & numDays
End Sub
Q13 VB program to find even and odd
numbers from an array.

Private Sub cmdClear_Click()


Form1.Cls
End Sub
Private Sub cmdDisplay_Click() Dim a() As
Integer
n = InputBox(“How many elements do u want to
enter”)
ReDim a(n)
Print “Array is : ” For i = 0 To n – 1
a(i) = Val(InputBox(“Enter the Elements”))
Print a(i) Next Print “Even Numbers are : ” For i
= 0 To n – 1 If a(i) Mod 2 = 0 Then Print a(i)
End If
Next
Print “Odd Numbers are : ”
For i = 0 To n – 1
If a(i) Mod 2 <> 0 Then Print a(i)
End If
Next
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Even no. Are:
2468
Odd no. Are:
1357

Q14.write a vb program to change textbox colour


Private Sub Command1_Click()
If Check1.Value = 1 Then
Text1 = "check is selected"
End If
End Sub

Private Sub Option1_Click()


Text1.ForeColor = vbRed
End Sub

Private Sub Option2_Click()


Text1.ForeColor = vbBlue
End Sub
Q15. A VB program to accept the number from the user
in text box and display multiplication table of that
number into the list box.
Private Sub Command1_Click() List1.Clear
Dim n As Integer, n = 0
Do While n < 12
n=n+1
r = Val(Text1.Text) * n
List1.AddItem (Text1.Text & " x " & n & " = " & r)
Loop
Text1.Text = ""
Text1.SetFocus
End Sub
Q16 Write a vb program to move a command button
using timer control

Private Sub Form_Load()


Timer1.Interval = 1
Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()


Command1.Left = Command1.Left + 2
End Sub
Q17. Design digital watch using design control

Private Sub Command1_Click()


Timer1.Enabled = True

End Sub

Private Sub Command2_Click()


Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()


Label1.Caption = Time

End Sub
Q18. Write a vb program to accept the details of the
book and store in database

Dim s As String

Private Sub Command1_Click()


r.movenext
If Not r.EOF Then
Text1.Text = r.fields(0)
Text2.Text = r.fields(1).Value
Text3.Text = r.fields(2).Value
Text4.Text = r.fields(3).Value
Else
MsgBox " no more records!", vbInformation, "book"
End If

End Sub

Private Sub Command2_Click()


Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""

Text1.Text.SetFocus

End Sub
Private Sub Command3_Click()
r.Close
s = "insert into book values(" & Val(Text1.Text) & ","
'&text2.text&"',"&text3.text"',"&val(text4.Text)&")"
r.open s, c, adopendynamic, adlockoptimistic
s = "select * from book"
r.open s, c, adopendynamic, adlockoptimistic
If Not r.bof And Not r.EOF Then
r.movefirst
Text1.Text = r.fields(0).Value
Text2.Text = r.fields(1).Value
Text3.Text = r.fields(2).Value
Text4.Text = r.fields(3).Value
End If
MsgBox "book added successfully!", vbInformation,
"book"

End Sub

Private Sub Command4_Click()


Dim id As Integer
id = Val(InputBox("enter book number"))

r.Close
s = "delete from book where bno like" '&id&""'
r.open s, c, adopendynamic, adlockoptimistic
s = "select*from book"
r.open s, c, adopendynamic, adlockoptimistic
If notr.bof And Not r.EOF Then
r.movefirst
Text1.Text = r.fields(0).Value
Text2.Text = r.fields(1).Value
Text3.Text = r.fields(2).Value
Text4.Text = r.fields(3).Value
End If
MsgBox ("record deleted successfully")

End Sub
Q19. Write a program for currency conversion

Dim pound As Integer


Dim dollar As Integer
Dim euro As Integer

Private Sub Command1_Click()


If Option1.Value = True Then
Label3.Caption = "dollar"
Text2.Text = Val(Text1.Text) / 71.26
ElseIf Option2.Value = True Then
Label3.Caption = "pound"
Text2.Text = Val(Text1.Text) / 91.8
ElseIf Option3.Value = True Then
Label3.Caption = "euro"
Text2.Text = Val(Text1.Text) / 81.03
Else
MsgBox ("selected conversion type")
End If
End Sub

Private Sub Command2_Click()


pound = Val(InputBox("91.8"))
dollar = Val(InputBox("71.26"))
euro = Val(InputBox("81.03"))
command11.Enabled = True

End Sub
Private Sub Form_Load()
Command2.Enabled = False

End Sub
Q20. Write a program to design traffic signals

Private Sub Timer1_Timer()


If Shape1.Visible Then

Shape2.Visible = True
Shape1.Visible = False
Shape3.Visible = False

ElseIf Shape2.Visible Then

Shape3.Visible = True
Shape2.Visible = False
Shape1.Visible = False

Else

Shape1.Visible = True
Shape2.Visible = False
Shape3.Visible = False
End If

End Sub

Potrebbero piacerti anche