[go: up one dir, main page]

0% found this document useful (0 votes)
36 views7 pages

VB Ex and Solutions

The document contains 12 exercises demonstrating basic VB.NET programming concepts like input/output, conditional statements, loops, arrays, sorting, etc. Each exercise provides sample code to perform tasks like arithmetic operations, random number generation, searching arrays, float addition, interest calculation, pattern printing, eligibility checking, number classification, sales calculation with discounts, bubble sorting, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

VB Ex and Solutions

The document contains 12 exercises demonstrating basic VB.NET programming concepts like input/output, conditional statements, loops, arrays, sorting, etc. Each exercise provides sample code to perform tasks like arithmetic operations, random number generation, searching arrays, float addition, interest calculation, pattern printing, eligibility checking, number classification, sales calculation with discounts, bubble sorting, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise 1:

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:

Write VB.NET program to generate a random number between 1 to 6.

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

Shopera mai Wilma@mundi- 2019


Console.ReadLine()
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 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

Shopera mai Wilma@mundi- 2019


Sub Main()
Dim amount_dep, rate, tax, interest_earned, tax_amount As Single
Console.Write("Enter the amount of deposit:")
amount_dep = Single.Parse(Console.ReadLine())
Console.Write("Enter yearly interest rate:")
rate = Single.Parse(Console.ReadLine())
interest_earned = amount_dep * (rate / 100) 'amount of interest
before tax calculation
Console.Write("Enter income tax rate:")
tax = Single.Parse(Console.ReadLine())
tax_amount = interest_earned * (tax / 100)
interest_earned -= tax 'the 􀃚nal interest earned
Console.WriteLine("The interest earned in the year:{0}",
interest_earned)
Console.ReadLine()
End Sub
End Module

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******

Shopera mai Wilma@mundi- 2019


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
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

Shopera mai Wilma@mundi- 2019


End Module
Exercise 9:
Write a VB.NET program to determine whether an input number is an even
number.
Module Module1
Sub Main()
Dim i As Integer
Console.Write("Enter a number:")
i = Integer.Parse(Console.ReadLine())
If (i Mod 2 = 0) Then
Console.WriteLine("It is an even number.")
Else
Console.WriteLine("It is an odd number.")
End If
Console.ReadLine()
End Sub
End Module

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

Shopera mai Wilma@mundi- 2019


revenue = unitprice * quantity
ElseIf (quantity >= 100 And quantity <= 120) Then
discount_rate = 10 / 100
revenue = unitprice * quantity
discount_amount = revenue * discount_rate
revenue -= discount_amount
ElseIf (quantity > 120) Then
discount_rate = 15 / 100
revenue = unitprice * quantity
discount_amount = revenue * discount_rate
revenue -= discount_amount
End If
Console.WriteLine("The revenue from sale:{0}$", revenue)
Console.WriteLine("After discount:{0}$({1}%)", discount_amount,
discount_rate * 100)
Console.ReadLine()
End Sub
End Module

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

Shopera mai Wilma@mundi- 2019


End Sub
End Module
Exercise 12:
Modify the VB.NET 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, 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
End Sub
End Module

Shopera mai Wilma@mundi- 2019

You might also like