Thought Sharing

Friday, December 11, 2009

Write a Program in VB to sort 10 numbers using an Array

Private Sub Command1_Click()


Dim Number(10) As Integer

Dim i As Long

Dim j As Long

Dim Temp As Integer

For i = 1 To 10

Number(i) = InputBox("Enter any number")

Next
For i = 1 To 10

For j = i To 10

If Number(i) > Number(j) Then

Temp = Number(i)

Number(i) = Number(j)

Number(j) = Temp

End If

Next j

Next i
For i = 1 To 10

Print Number(i)

Next


End Sub

Wednesday, December 9, 2009

Write a program in VB to calculate average of 10 numbers using arrays

Private Sub Command1_Click()
Dim n(10) As Integer
Dim sum As Long
Dim Avg As Single
sum = 0
For i = 1 To 10
n(i) = InputBox("Enter number")
Next
For i = 1 To 10
sum = sum + n(i)
Next
Print sum
Avg = sum / 10
Print Avg
End Sub

Monday, March 23, 2009

Write a program in Visual Basic to check whether a string is Palindrome or not.

Write a program in Visual Basic to check whether a string is Palindrome or not.

A palindrome is any "word" which is the same forward and backward e.g. amma, radar, noon, 20011002 etc

Step-1 Drag & drop one Text box and one Command button on form.
Step-2 Write following code on the click event of command button:

Private Sub Command1_Click()
a = StrReverse(Text1.Text)
If a = Text1.Text Then
MsgBox "Given string is Palindrome"
Else
MsgBox "String is Not Palindrome"
End If
End Sub

Program in Visual Basic to generate the following pattern of alphabets:

Program in Visual Basic to generate the following pattern of alphabets:

E E E E E
D D D D
C C C
B B
A
Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 69 To 65 Step -1
For j = 65 To i
Print Chr(i);
Next j
Print
Next i
End Sub

Program in Visual Basic to generate the following pattern of alphabets:

Program in Visual Basic to generate the following pattern of alphabets:

A
B B
C C C
D D D D
E E E E E

Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 65 To 69 Step 1
For j = 65 To i
Print Chr(i);
Next j
Print
Next i
End Sub

Program in Visual Basic to generate the following pattern of alphabets:

Program in Visual Basic to generate the following pattern of stars:

A B C D E
A B C D
A B C
A B
A

Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 69 To 65 Step -1
For j = 65 To i
Print Chr(j);
Next j
Print
Next i

End Sub

Program in Visual Basic to generate the following pattern of alphabets:

Program in Visual Basic to generate the following pattern of alphabets:
A
A B
A B C
A B C D
A B C D E
Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 65 To 69 Step 1
For j = 65 To i
Print Chr(j);
Next j
Print
Next i

End Sub

Program in Visual Basic to generate the following pattern of stars:

Program in Visual Basic to generate the following pattern of stars:

* * * * *
* * * *
* * *
* *
*

Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 5 To 1 Step -1
For j = 1 To i
Print " * ";
Next j
Print
Next i

End Sub

Program in Visual Basic to generate the following pattern of stars:

Program in Visual Basic to generate the following pattern of stars:

*
* *
* * *
* * * *
* * * * *

Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer
For i = 1 To 5 Step 1
For j = 1 To i
Print " * ";
Next j
Print
Next i

End Sub

Friday, March 20, 2009

Write a program in Visual Basic to design traffice Light



Program in Visual Basic to design Traffic Light

Step1 Open a new project in VB IDE
Step2 Add three shape control (Shape1, Shape2, and Shape3) from Tool Box on the form. Change Back Style Property of each Shape control to Opaque (By property window).
Step3 Drag & drop a Timer control on the form and Set its Interval Property to some Integer value (say 3000).
Step4 Now write the following code on the Timer Event of Timer Control (Timer1)

Private Sub Timer1_Timer()
If Shape3.BackColor = vbGreen Then
Shape2.BackColor = vbYellow
Shape3.BackColor = vbWhite
Timer1.Interval = 500
ElseIf Shape2.BackColor = vbYellow Then
Shape1.BackColor = vbRed
Shape2.BackColor = vbWhite
Timer1.Interval = 5000
Else
Shape3.BackColor = vbGreen
Shape1.BackColor = vbWhite
Timer1.Interval = 3500
End If
End Sub