Visual Basic

Detect errors and display output

1. label1.text= “Hello there”

2. For count = 10 to 1 step -1                                                                                                                      label1.text= label1.text + str(count)                                                                                                 Next

3. Dim num as Integer

num = InputBox(“Please type in a number:”)
If num <= -1 Then
Label3.Text = Val(num) & ” ” & “is a negative number”                                                             Else                                                                                                                                                                  Label3.text= val(num) & ” ” & “is a positive number”
End If

3

This is #3 step 1

33

This is #3 step 2

 

4. Display output

SHOULD DISPLAY THE FOLLOWING:

25 10 125 8 625 6 3125 4 15625 2

exam

this is for the Display output and #2

 

الإعلانات
قياسي
Visual Basic

Decision and Branching

decisionbranching.png

Public Class frmDecisionsandBranching
Dim Horsepower As Integer, Num As Integer, Age As Integer

‘ **************************************************************
‘                                                   btnAge.click
‘ **************************************************************
Private Sub btnAge_Click(sender As System.Object, e As System.EventArgs) Handles btnAge.Click
Age = InputBox(“Please enter your age:”, “Age”)
If Age >= 18 Then
lblAge.Text = “You are old enough to vote!”
ElseIf Age < 18 Then
lblAge.Text = “You are not old enough to vote, you need to wait for ” & 18 – Val(Age) & ” more years to vote.”
End If
End Sub

 

‘ **************************************************************
‘                                        btnHorsePower.click
‘ **************************************************************
Private Sub btnHorsePower_Click(sender As System.Object, e As System.EventArgs) Handles btnHorsePower.Click
Horsepower = InputBox(“Please enter the horsepower of your vehicle:”, “Horsepower”)
If Horsepower > 300 Then
lblHorsePower.Text = “You need to pay for $500.00!”
ElseIf Horsepower >= 200 Then
lblHorsePower.Text = “You need to pay for $150.00 fee!”
ElseIf Horsepower >= 100 Then
lblHorsePower.Text = “You need to pay for $70.00 fee!”
ElseIf Horsepower > 50 Then
lblHorsePower.Text = “You need to pay for $30.00 fee!”
ElseIf Horsepower <= 50 Then
lblHorsePower.Text = “You don’t have to pay the fee!”
End If
End Sub

 

‘ **************************************************************
‘                                             btnNum.click
‘ **************************************************************
Private Sub btnNum_Click(sender As System.Object, e As System.EventArgs) Handles btnNum.Click
Num = InputBox(“Please enter the number:”, “Num”)
If Num > 0 Then
lblNum.Text = “The numeber you have typed is positive.”
ElseIf Num < 0 Then
lblNum.Text = “The number you have typed is negative. ”
ElseIf Num = 0 Then
lblNum.Text = “The number you have typed is neutral.”
End If
End Sub

قياسي
Visual Basic

Sub-Strings

sub-strings

Exercise 2: “Write a program that will allow the user to enter two words and then will determine and display which word is longer.”

Public Class Form1
Dim Word1 As String, Word2 As String, Sentence As String, Ecounter As Integer, Backward As String
Dim Counter As Integer, SpaceCounter As Integer, TotalCounter As Integer
Private Sub btnExercise2_Click(sender As System.Object, e As System.EventArgs) Handles btnExercise.Click
Word1 = InputBox(“Please enter word 1:”)
Word2 = InputBox(“Please enter word 2:”)
If Len(Word1) > Len(Word2) Then
lblDisplay.Text = “Word1 is longer than Word2.”
ElseIf Len(Word1) < Len(Word2) Then
lblDisplay.Text = “Word2 is longer than Word1.”
Else
lblDisplay.Text = “Word1 is equal to Word2.”
End If
End Sub

Exercise 3“Write a program which allows an input of a three character string such as CAT and prints the three letters vertically as follows:

C

A

T

Private Sub btnExercise3_Click(sender As System.Object, e As System.EventArgs) Handles btnExercise3.Click
Word1 = InputBox(“Please enter 3 character word such as CAT:”)
lblDisplay.Text = Microsoft.VisualBasic.Left(Word1, 1)
lblDisplay2.Text = Mid(Word1, 2, 1)
lblDisplay3.Text = Microsoft.VisualBasic.Right(Word1, 1)
End Sub

Exercise 4: “Write a program that allows a user to input a sentence then counts the number of occurrences of the letter E.”

Private Sub btnExercise4_Click(sender As System.Object, e As System.EventArgs) Handles btnExercise4.Click
Sentence = InputBox(“Please type in a sentence:”)
Ecounter = 0
Sentence = LCase(Sentence)
For x = 1 To Len(Sentence)
If Mid(Sentence, x, 1) = “e” Then
Ecounter = Ecounter + 1
End If
Next
lblDisplay2.Text = “The total # of e’s: ” & Ecounter
End Sub

Exercise 5: “A palindrome is a word that reads the same forward as backwards. For example, LEVEL, DAD or TOOT. Write a program which determines if a word entered by a user is a palindrome or not.”

Private Sub btnExercise5_Click(sender As System.Object, e As System.EventArgs) Handles btnExercise5.Click
Word1 = InputBox(“Please enter a word:”)
Backward = StrReverse(Word1)
If Word1 = StrReverse(Word1) Then
lblDisplay2.Text = “This word is a palindrome.”
Else
lblDisplay2.Text = “This word is not a palindrome.”
End If
End Sub

Exercise 6: “Write a program that allows a user to input a sentence and then tells us the user how many words were entered.”

Private Sub btnExercise6_Click(sender As System.Object, e As System.EventArgs) Handles btnExercise6.Click
Sentence = InputBox(“Please type in a sentence:”)
SpaceCounter = 0
Sentence = LCase(Sentence)
For x = 1 To Len(Sentence)
If Mid(Sentence, x, 1) = ” ” Then
SpaceCounter = SpaceCounter + 1
End If
Next
lblDisplay2.Text = “The total words entered: ” & SpaceCounter + 1
End Sub
End Class

قياسي
Visual Basic

Lotto649

lottoTHIS IS A BUBBLE SORT EXAMPLE!

Exercise: “Create a lotto 649 program. Your program must be able to allow user to chose the number of tickets he/she desires and then produces the tickets displaying the numbers of each ticket in ascending order (smallest to largest) 

*NOTE: ticket must not have double numbers.”

Public Class Form1
Dim num(20) As Integer
Dim temp As Integer
Dim Tickets As Integer

Private Sub btnLotto_Click(sender As System.Object, e As System.EventArgs) Handles btnLotto.Click
lblDisplay.Text = ” ”

Tickets = InputBox(“How many tickets are you buying?”)
For a = 1 To Tickets

PickTickets()
SortNums()
DisplaySort()
Next

End Sub
Public Sub PickTickets()
For x = 1 To 6
Num(x) = Int((49 – 1 + 1) * Rnd() + 1)
Next
End Sub
Public Sub SortNums()
For x = 1 To 5
For y = x + 1 To 6
If Num(x) < Num(y) Then
temp = Num(x)
Num(x) = Num(y)
Num(y) = temp
ElseIf Num(x) = Num(y) Then
Num(x) = Int((49 – 1 + 1) * Rnd() + 1)
x = 0
End If
Next y
Next x

End Sub

Public Sub DisplaySort()
For x = 1 To 6
lblDisplay.Text = lblDisplay.Text & Num(x) & ” ”
Next
lblDisplay.Text = lblDisplay.Text & vbNewLine

End Sub
End Class

قياسي
Visual Basic

Loop

lotto

-> 1st LOOP: “Write a program that will compute and display the sum of all whole numbers between 1 and 100 inclusive. (Solve sing the do while… loop or do…. loop while)

Public Class Form1
Dim Num1 As Double
Dim Num2 As Double
Dim Num3 As Double
Dim Counter As Single
Dim Runtotal As Single
Dim FirstNumber As Integer
Dim SecondNumber As Integer

Private Sub btnLoop1_Click(sender As System.Object, e As System.EventArgs) Handles       btnLoop1.Click
Counter = 0
Runtotal = 0
Do
Counter = Counter + 1
Runtotal = Runtotal + Counter
lblDisplay.Text = lblDisplay.Text & Counter & “+”
Loop While Counter < 99
Counter = Counter + 1
Runtotal = Runtotal + Counter
lblDisplay.Text = lblDisplay.Text & Counter & “=” & Runtotal
End Sub

->2nd LOOP: “There is an interesting sequence of numbers called the Fibonacci Numbers. The set begins with 0 and 1. Then each succeeding number in the sequence is the sum of the previous two. Thus, the Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13… and so on. Write a program that will compute the first twenty numbers of this sequence and will display them. (Solve using the do until… loop or do … loop until)

Private Sub btnLoop2_Click(sender As System.Object, e As System.EventArgs) Handles btnLoop2.Click
Num1 = 0
Num2 = 1
Num3 = Num1 + Num2
lblDisplay2.Text = Num1 & ” ” & Num2 & ” ” & Num3
Do
Counter = Counter + 1
Num1 = Num2
Num2 = Num3
Num3 = Num1 + Num3
lblDisplay2.Text = lblDisplay2.Text & ” ” & Num3
Loop Until Counter = 17
End Sub

->3rd LOOP: “Write a program that will allow the user to input two numbers. The program must then display the numbers in increments or decrements of 2. Remember, the program must determine if the first number is less than or greater than the second number so that the display will occur. (Solve using the For-Next loop structure).

Private Sub btnLoop3_Click(sender As System.Object, e As System.EventArgs) Handles btnLoop3.Click
lblDisplay3.Text = “”
FirstNumber = InputBox(“Please enter the first number”, “First Number Input”)
SecondNumber = InputBox(“Please enter the second number”, “Second Number Input”)
If FirstNumber < SecondNumber Then For x = FirstNumber To SecondNumber Step 2 lblDisplay3.Text = lblDisplay3.Text & x & ” ” Next x ElseIf FirstNumber > SecondNumber Then
For x = FirstNumber To SecondNumber Step -2
lblDisplay3.Text = lblDisplay3.Text & x & ” ”
Next x
Else
lblDisplay3.Text = “Numbers are equal.”
End If
End Sub
End Class

قياسي