VB Ex and Solutions
VB Ex and Solutions
Write VB.NET 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.
Dim a As Integer
Dim b As Integer
Console.Write("Enter value a:")
a = Integer.Parse(Console.ReadLine())
Console.Write("Enter value b:")
b = Integer.Parse(Console.ReadLine())
Console.WriteLine("The result of adding is {0}.", a + b)
Console.WriteLine("The result of subtracting is {0}.", a - b)
Console.WriteLine("The result of multiplying is {0}.", a * b)
Console.WriteLine("The result of dividing is {0}.", a / b)
Exercise 2:
Module Module1
Sub Main()
Dim rdn As Integer
Dim rdnObject = New Random()
Randomize()
rdn = 1 + rdnObject.Next Mod 6
Console.WriteLine("The Result:{0}", rdn)
Console.ReadLine()
End Sub
End Module
Exercise 3
By using the sequential search algorithm, write VB.NET code to search for
an element of an integer array of 10 elements.
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, arr.Length)
If (index <> -1) Then
Console.WriteLine("The item was found at the position {0}.", index)
End If
Exercise 4:
Write VB.NET program to allow the user to input two _oat values and then
the program adds the two values together.
Module Module1
Sub Main()
Dim a As Single
Dim b As Single
Console.Write("Enter value a:")
a = Single.Parse(Console.ReadLine())
Console.WriteLine("The value of a before adding: {0}", a)
Console.Write("Enter value b:")
b = Single.Parse(Console.ReadLine())
a=a+b
Console.WriteLine("The value of a after adding:{0}", a)
Console.ReadLine()
End Sub
Exercise 5:
Write VB.NET program to allow the user to input the amount of deposit,
yearly interest rate (percentage), and income tax(percentage).
Module Module1
Exercise 6:
Write VB.NET code to produce the output shown below:
*******
******
*****
****
***
**
*
Module Module1
Sub Main()
Dim i, j As Integer
For i = 0 To 6
For j = 1 To 7 - i
Console.Write("*")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Exercise 7:
Write VB.NET code to print the following pattern:
1******
Solution:
Module Module1
Sub Main()
Dim i, j, k As Integer
For i = 1 To 7
For j = 1 To i
Console.Write(j)
Next
For k = 7 - i To 1 Step -1
Console.Write("*")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Exercise 8:
Write VB.NET 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
Console.Write("Enter your age:")
age = Integer.Parse(Console.ReadLine())
If (age >= 18) Then
Console.WriteLine("You are eligible to vote.")
Else
Console.WriteLine("You are not eligible to vote.")
End If
Console.ReadLine()
End Sub
Exercise 10:
Write a VB.NET 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%)
Solution:
Module Module1
Sub Main()
Dim unitprice As Single = 0.0F
Dim quantity As Integer = 0
Dim revenue As Single = 0.0F
Dim discount_rate As Single = 0.0F
Dim discount_amount As Single = 0.0F
Console.Write("Enter unit price:")
unitprice = Single.Parse(Console.ReadLine())
Console.Write("Enter quantity:")
quantity = Single.Parse(Console.ReadLine())
If (quantity < 100) Then
Exercise 11:
By using the bubble sort algorithm, write VB.NET 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, arr.Length)
Dim i As Integer
For i = 0 To arr.Length - 1
Console.Write("{0,4}", arr(i))
Next
Console.ReadLine()
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