Visual Basic or VB.
NET variables exercises
Exercise 1: Write [Link] code to declare a variable to store
the age of a person.
Then the output of the program is as an example shown below:
You are 20 years old.
Solution:
Module Module1
Sub Main()
Dim age As Integer = 20 'declaring variable and assign 20 to it.
[Link]("You are " & age & " years old.")
[Link]()
End Sub
End Module
Exercise 2:Write [Link] code to display the asterisk pattern
as shown below:
*****
*****
*****
*****
*****
Solution:
Module Module1
Sub Main()
[Link]("*****")
[Link]("*****")
[Link]("*****")
[Link]("*****")
[Link]("*****")
[Link]()
End Sub
End Module
For Each...Next, Exit, Continue Statement in [Link] with
EXAMPLE
Exercise 3: Write [Link] code to declare two integer
variables, one float variable, and one string variable and
assign 10, 12.5, and "[Link] programming" to them
respectively.
Then display their values on the screen.
Solution:
Module Module1
Sub Main()
Dim x As Integer
Dim y As Single
Dim s As String
x = 10
y = 12.5F
s = "I like VB programming."
[Link](x)
[Link](y)
[Link](s)
[Link]()
End Sub
End Module
Exercise 4: Write [Link] code to prompt a user to input
his/her name and then the output will be shown as an
example below:
Hello John!
Solution:
Module Module1
Sub Main()
Dim name As String
[Link]("Please enter your name:")
name = [Link]()
[Link]("Hello {0}!", name)
[Link]()
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
ARITHMETIC OPERATORS
[Link] arithmetic operators
Exercise 1: Write [Link] program to allow the user to input
two integer values and then the program print the results of
adding, subtracting, multiplying, and dividing among the two
values.
See the example below:
Enter value a:30
Enter value b:10
The result of adding is 40.
The result of subtracting is 20;
The result of multiplying is 300.
The result of dividing is 3.
Solution:
Module Module1
Sub Main()
Dim a As Integer
Dim b As Integer
[Link]("Enter value a:")
a = [Link]([Link]())
[Link]("Enter value b:")
b = [Link]([Link]())
[Link]("The result of adding is {0}.", a + b)
[Link]("The result of subtracting is {0}.", a - b)
[Link]("The result of multiplying is {0}.", a * b)
[Link]("The result of dividing is {0}.", a / b)
[Link]()
End Sub
End Module
Exercise 2: Write [Link] program to generate a random
number between 1 to 6.
To generate a random number, you can use the Random class .
Solution:
Module Module1
Sub Main()
Dim rdn As Integer
Dim rdnObject = New Random()
Randomize()
rdn = 1 + [Link] Mod 6
[Link]("The Result:{0}", rdn)
[Link]()
End Sub
End Module
Exercise 1:Write [Link] program to allow the user to input
two float values and then the program adds the two values
together.
The result will be assigned to the first variable.
Enter value a:12.5
The value of a before adding is 12.5.
Enter value b:34.9
The value of a after adding is 47.4.
Solution:
Module Module1
Sub Main()
Dim a As Single
Dim b As Single
[Link]("Enter value a:")
a = [Link]([Link]())
[Link]("The value of a before adding: {0}", a)
[Link]("Enter value b:")
b = [Link]([Link]())
a=a+b
[Link]("The value of a after adding:{0}", a)
[Link]()
End Sub
End Module
Exercise 2: Write [Link] program to allow the user to input
the amount of deposit, yearly interest rate (percentage), and
income tax(percentage).
Then the program will calculate the amount of interest that the person earns in
the year. See the example output below:
The amount of deposit: 1000
Yearly interest rate: 7.5%
Income tax rate: 4%
The amount of interest earned in the year:71.0
Solution:
Module Module1
Sub Main()
Dim amount_dep, rate, tax, interest_earned, tax_amount As Single
[Link]("Enter the amount of deposit:")
amount_dep = [Link]([Link]())
[Link]("Enter yearly interest rate:")
rate = [Link]([Link]())
interest_earned = amount_dep * (rate / 100) 'amount of interest before tax
calculation
[Link]("Enter income tax rate:")
tax = [Link]([Link]())
tax_amount = interest_earned * (tax / 100)
interest_earned -= tax 'the final interest earned
[Link]("The interest earned in the year:{0}", interest_earned)
[Link]()
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
IF ELSE AND COMPARISON
OPERATORS
[Link] if else and comparison operators
Exercise 1: Write [Link] program to allow the user to input
his/her age.
Then the program will show if the person is eligible to vote. A person who is
eligible to vote must be older than or equal to 18 years old.
Enter your age: 18
You are eligible to vote.
Solution:
Module Module1
Sub Main()
Dim age As Integer
[Link]("Enter your age:")
age = [Link]([Link]())
If (age >= 18) Then
[Link]("You are eligible to vote.")
Else
[Link]("You are not eligible to vote.")
End If
[Link]()
End Sub
End Module
Exercise 2: Write a [Link] program to determine whether an
input number is an even number.
Module Module1
Sub Main()
Dim i As Integer
[Link]("Enter a number:")
i = [Link]([Link]())
If (i Mod 2 = 0) Then
[Link]("It is an even number.")
Else
[Link]("It is an odd number.")
End If
[Link]()
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
IF ELSE AND LOGICAL OPERATORS
[Link] if else and logical operators
Exercise 1: Write a Java program that determines a student’s
grade.
The program will read three types of scores(quiz, mid-term, and final scores)
and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
See the example output below:
Quiz score: 80
Mid-term score: 68
Final score: 90
Your grade is B.
Solution:
Module Module1
Sub Main()
Dim quiz_score, mid_score, final_score, avg As Single
[Link]("Enter quiz score:")
quiz_score = [Link]([Link]())
[Link]("Enter mid-term score:")
mid_score = [Link]([Link]())
[Link]("Enter final score:")
final_score = [Link]([Link]())
avg = (quiz_score + mid_score + final_score) / 3
If avg >= 90 Then
[Link]("Grade A")
ElseIf avg >= 70 And avg < 90 Then
[Link]("Grade B")
ElseIf avg >= 50 And avg < 70 Then
[Link]("Grade C")
ElseIf avg < 50 Then
[Link]("Grade F")
Else : [Link]("Invalid input")
End If
[Link]()
End Sub
End Module
Exercise 2: Write a [Link] program to calculate the revenue
from a sale based on the unit price and quantity of a product
input by the user.
The discount rate is 10% for the quantity purchased between 100 and 120
units, and 15% for the quantity purchased greater than 120 units. If the
quantity purchased is less than 100 units, the discount rate is 0%. See the
example output as shown below:
Enter unit price: 25
Enter quantity: 110
The revenue from sale: 2475.0$
After discount: 275.0$(10.0%)
[Link] EXERCISES AND SOLUTIONS:
FOR LOOP
[Link] for loop
Exercise 1:Write [Link] code to produce the output shown
below:
*******
******
*****
****
***
**
Solution:
Module Module1
Sub Main()
Dim i, j As Integer
For i = 0 To 6
For j = 1 To 7 - i
[Link]("*")
Next
[Link]()
Next
[Link]()
End Sub
End Module
Exercise 2: Write [Link] code to print the following pattern:
1******
12*****
123****
1234***
12345**
123456*
1234567
Solution:
Module Module1
Sub Main()
Dim i, j, k As Integer
For i = 1 To 7
For j = 1 To i
[Link](j)
Next
For k = 7 - i To 1 Step -1
[Link]("*")
Next
[Link]()
Next
[Link]()
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
WHILE LOOP
[Link] while loop
Exercise 1: Write [Link] program to prompt the user to
choose the correct answer from a list of answer choices of a
question.
The user can choose to continue answering the question or stop answering it.
See the example below:
What is the command keyword to exit a loop in [Link]?
a. int
b. continue
c. break
d. Exit For/Exit While/Exit Do While
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:
Module Module1
Sub Main()
Dim ch, con As String
con = "y"
[Link]("What is the correct way to declare a variable to store an
integer value in [Link]?")
[Link]("a. Int")
[Link]("b. Continue")
[Link]("c. Break")
[Link]("d. Exit For/Exit While/Exit Do While")
While [Link](con, "y") = 0
[Link]("Enter your choice:")
ch = [Link]().ToString()
If ([Link](ch, "d") = 0) Then
[Link]("Congratulation!")
Else : [Link]("Incorrect!")
End If
[Link]("Continue? Press y to continue:")
con = [Link]().ToString()
End While
[Link]()
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
DO WHILE LOOP
[Link] do while loop
Exercise 1: By using do while loop, write [Link] program to
prompt the user to choose the correct answer from a list of
answer choices of a question.
The user can choose to continue answering the question or stop answering it.
See the example below:
What is the correct way to declare a variable to an integer value in [Link]?
a. int a
b. Dim a As Single
c. Dim a As String
d. Dim a As Integer
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:
Module Module1
Sub Main()
Dim ch, con As String
con = "y"
[Link]("What is the correct way to declare a variable to store an
integer value in [Link]?")
[Link]("a. int a")
[Link]("b. Dim a as Single")
[Link]("c. Dim a as String")
[Link]("d. Dim a as Integer")
Do While [Link](con, "y") = 0
[Link]("Enter your choice:")
ch = [Link]().ToString()
If ([Link](ch, "d") = 0) Then
[Link]("Congratulation!")
Else : [Link]("Incorrect!")
End If
[Link]("Continue? Press y to continue:")
con = [Link]().ToString()
Loop
[Link]()
End Sub
End Module
Exercise 2: By using do while loop, write [Link] program to
print the table of characters that are equivalent to the Ascii
codes from 1 to [Link] program will print the 10 characters
per line.
[Link] EXERCISES AND SOLUTIONS:
DO WHILE LOOP
[Link] do while loop
Exercise 1: By using do while loop, write [Link] program to
prompt the user to choose the correct answer from a list of
answer choices of a question.
The user can choose to continue answering the question or stop answering it.
See the example below:
What is the correct way to declare a variable to an integer value in [Link]?
a. int a
b. Dim a As Single
c. Dim a As String
d. Dim a As Integer
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:
Module Module1
Sub Main()
Dim ch, con As String
con = "y"
[Link]("What is the correct way to declare a variable to store an
integer value in [Link]?")
[Link]("a. int a")
[Link]("b. Dim a as Single")
[Link]("c. Dim a as String")
[Link]("d. Dim a as Integer")
Do While [Link](con, "y") = 0
[Link]("Enter your choice:")
ch = [Link]().ToString()
If ([Link](ch, "d") = 0) Then
[Link]("Congratulation!")
Else : [Link]("Incorrect!")
End If
[Link]("Continue? Press y to continue:")
con = [Link]().ToString()
Loop
[Link]()
End Sub
End Module
Exercise 2: By using do while loop, write [Link] program to
print the table of characters that are equivalent to the Ascii
codes from 1 to [Link] program will print the 10 characters
per line.
[Link] EXERCISES AND SOLUTIONS:
[Link] ARRAY
[Link] Array
Exercise 1: By using the bubble sort algorithm, write [Link]
code to sort an integer array of 10 elements in ascending.
Solution:
Module Module1
Sub Main()
Dim arr() As Integer = {12, 23, 1, 21, 12, 12, 32, 45, 3, 5}
bubbleSort(arr, [Link])
Dim i As Integer
For i = 0 To [Link] - 1
[Link]("{0,4}", arr(i))
Next
[Link]()
End Sub
Sub bubbleSort(ByVal dataset() As Integer, ByVal n As Integer)
Dim i, j As Integer
For i = 0 To n Step 1
For j = n - 1 To i + 1 Step -1
If (dataset(j) < dataset(j - 1)) Then
Dim temp As Integer = dataset(j)
dataset(j) = dataset(j - 1)
dataset(j - 1) = temp
End If
Next
Next
End Sub
End Module
Exercise 2: Modify the [Link] code in exercise 1 to sort the
array in descending order.
Solution:
Module Module1
Sub Main()
Dim arr() As Integer = {12, 23, 1, 21, 12, 12, 32, 45, 3, 5}
bubbleSort(arr, [Link])
Dim i As Integer
For i = 0 To [Link] - 1
[Link]("{0,4}", arr(i))
Next
[Link]()
End Sub
Sub bubbleSort(ByVal dataset() As Integer, ByVal n As Integer)
Dim i, j As Integer
For i = 0 To n Step 1
For j = n - 1 To i + 1 Step -1
If (dataset(j) > dataset(j - 1)) Then
Dim temp As Integer = dataset(j)
dataset(j) = dataset(j - 1)
dataset(j - 1) = temp
End If
Next
Next
End Sub
End Module
[Link] EXERCISES AND SOLUTIONS:
ARRAY SEARCH
[Link] Array
Exercise 1: By using the sequential search algorithm, write
[Link] code to search for an element of an integer array of
10 elements.
Solution:
Module Module1
Sub Main()
Dim arr() As Integer = {12, 23, 1, 21, 12, 12, 32, 45, 3, 5}
Dim index As Integer
index = seqSearch(arr, 1, [Link])
If (index <> -1) Then
[Link]("The item was found at the position {0}.", index)
End If
[Link]()
End Sub
Function seqSearch(ByVal dataset() As Integer, ByVal target As Integer,
ByVal n As Integer) As Integer
Dim i, found, pos As Integer
found = 0
pos = -1
For i = 1 To n Step 1
If (found <> 1) Then
If (target = dataset(i)) Then
pos = i
found = 1
End If
End If
Next
Return pos
End Function
End Module
Exercise 2: Modify the [Link] code in exercise 1 to search
for an element of the array by using binary search algorithm.
Solution:
Module Module1
Sub Main()
Dim arr() As Integer = {12, 23, 1, 21, 12, 12, 32, 45, 3, 5}
Dim index, i As Integer
bubbleSort(arr, [Link]) 'make sure the data items are sorted
[Link]("The array after being sorted:")
For i = 0 To [Link] - 1
[Link]("{0,4}", arr(i))
Next
[Link]()
index = binSearch(arr, 1, [Link])
If (index <> -1) Then
[Link]("The item was found at the position {0}.", index)
End If
[Link]()
End Sub
Function binSearch(ByVal dataset() As Integer, ByVal target As Integer, ByVal
n As Integer) As Integer
Dim i, found, pos As Integer
found = 0
pos = -1
If (target < dataset(n / 2)) Then 'look in the firt half
For i = 0 To n / 2 Step 1
If (found <> 1) Then
If (target = dataset(i)) Then
found = 1
pos = i
End If
End If
Next
Else 'look in the second half
For i = n / 2 To n Step 1
If (found <> 1) Then
If (target = dataset(i)) Then
found = 1
pos = i
End If
End If
Next
End If
Return pos
End Function
Sub bubbleSort(ByVal dataset() As Integer, ByVal n As Integer)
Dim i, j As Integer
For i = 0 To n Step 1
For j = n - 1 To i + 1 Step -1
If (dataset(j) < dataset(j - 1)) Then
Dim temp As Integer = dataset(j)
dataset(j) = dataset(j - 1)
dataset(j - 1) = temp
End If
Next
Next
End Sub
End Module