Lab Maunal Solutions-2
Lab Maunal Solutions-2
Imports System
Module Program
Sub Main()
Console.WriteLine("Programming is interesting")
End Sub
End Module
-------------------------------------------------------------------
Activity 2.2
Program:
Imports System
Module Program
Sub Main()
Dim message As String
Console.Write("Enter Message: ")
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Your message: {0}", message)
Console.ReadLine()
End Sub
End Module
Output:
--------------------------------------------------------------------
1. Open Visual Studio and create a new VB.Net project by going to File > New >
Project.
2. Select the type of project you want to create (e.g., Console Application,
Windows Forms Application).
3. Write your VB.Net code in the editor window.
4. To compile the program, go to Build > Build Solution or simply press Ctrl +
Shift + B.
5. If there are no errors in your code, the program will be compiled successfully.
6. To execute the program, go to Debug > Start Debugging or press F5.
7. The program will run, and you will see the output in the Output window or the
console window, depending on the type of application you created.
-------------------------------------------------------------------
Imports System
Module Program
Sub Main()
Dim name, matric_no, email, address, hod_name, institution, phone As String
Dim d_o_b As Date
End Sub
End Module
----------------------------------------------------------------------
Activity 3.1
Imports System
Module Program
Sub Main()
Dim si As Single
Dim dt As Date
Dim d As Double
Dim c As Char
Dim s As String
si = 0.12345678901234566
d = 0.12345678901234566
dt = Today
c = "U"
s = "VB.Net"
Console.Write(c & " and " & s & vbCrLf)
Console.WriteLine("declaring on the day of: {0}", dt)
Console.WriteLine("We will learn VB.Net seriously")
Console.WriteLine("Let us see what happens to the floating point variables:
")
Console.WriteLine("The Single: {0}, The Double: {1}", si, d)
Console.ReadKey()
End Sub
End Module
--------------------------------------------------------------------------
Activity 3.2
Imports System
Module Program
Sub Main()
Dim i As Integer
Dim dt As Date
Dim c As Char
Dim s As String
i = 20
dt = Today
c = "U"
s = "VB.Net"
'the oath taking
Console.Write(c & " and " & s & vbCrLf)
Console.WriteLine("declaring on the day of: {0}", dt)
Console.WriteLine("We will learn VB.Net seriously")
Console.WriteLine("Let us see what happens to the floating point variables:
")
Console.WriteLine("The Integer: {0}", i)
Console.ReadKey()
End Sub
End Module
Output:
U and VB.Net
declaring on the day of: 30/04/2024 00:00:00
We will learn VB.Net seriously
Let us see what happens to the floating point variables:
The Integer: 20
----------------------------------------------------------------------------
Activity 3.3
Imports System
Module Program
Sub Main()
Dim a As Integer = 20
Dim b As Integer = 28
Dim p As Integer = 4
Dim c As Integer
Dim d As Single
c = a + b
Console.WriteLine("Line 1 - Value of c is {0}", c)
d = a - b
Console.WriteLine("Line 2 - Value of d is {0}", d)
c = a * b
Console.WriteLine("Line 3 - Value of c is {0}", c)
d = a / b
Console.WriteLine("Line 4 - Value of d is {0}", d)
c = a \ b
Console.WriteLine("Line 5 - Value of c is {0}", c)
c = a Mod b
Console.WriteLine("Line 6 - Value of c is {0}", c)
c = b ^ p
Console.WriteLine("Line 7 - Value of c is {0}", c)
Console.ReadLine()
End Sub
End Module
Output:
Line 1 - Value of c is 48
Line 2 - Value of d is -8
Line 3 - Value of c is 560
Line 4 - Value of d is 0.71428573
Line 5 - Value of c is 0
Line 6 - Value of c is 20
Line 7 - Value of c is 614656
------------------------------------------------------------------------
1. Char | 2 byte
2. Integer | 4 bytes
3. Double | 8 bytes
4. Decimal | 16 bytes
5. Date | 8 bytes
------------------------------------------------------------------------
Step 1: Start
Step 2: Declare a constant to store the value of PI and assign the value 3.14 to PI
Step 3: Declare a variable for Radius and assign the value 8 to it.
Step 4: Declare another variable for Area and assign as value the outcome from the
aritmetic expression: PI * (radius ^ 2) to it.
Step 5: Print the value of Area variable to Screen
Step 6: End
(program)
Imports System
Module Program
Sub Main()
Const PI As Double = 3.14
Dim radius As Double = 8
Dim area_of_circle As Double
area_of_circle = PI * (radius ^ 2)
Console.WriteLine("The area of a circle with radius 8 = {0}",
area_of_circle)
End Sub
End Module
------------------------------------------------------------------------
------------------------------------------------------------------------
Imports System
Module Program
Sub Main()
Dim a As Integer = 42
Dim b As Integer = 26
Dim p As Integer = 6
Dim c As Integer
Dim d As Single
Console.WriteLine("Line 3 - Value of c is {0}", c)
d = a / b
Console.WriteLine("Line 4 - Value of d is {0}", d)
c = a \ b
Console.WriteLine("Line 5 - Value of c is {0}", c)
c = a Mod b
Console.WriteLine("Line 6 - Value of c is {0}", c)
c = b ^ p
Console.WriteLine("Line 7 - Value of c is {0}", c)
Console.ReadLine()
End Sub
End Module
Output:
Line 3 - Value of c is 0
Line 4 - Value of d is 1.6153846
Line 5 - Value of c is 1
Line 6 - Value of c is 16
Line 7 - Value of c is 308915776
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Activity 4.1
Module Program
Sub Main()
Dim colour As String
Dim red As String
If colour = red Then
Console.WriteLine("You have chosen a red car!")
End If
Console.WriteLine("the colour you have chosen is: {0}", colour)
Console.ReadLine()
End Sub
End Module
----------------------------------------------------------------------
Activity 4.2
Program:
Imports System
Module Program
Sub Main()
Dim colour As String
Dim red As String
If colour = red Then
Console.WriteLine("You have chosen a red car!")
Else
Console.WriteLine("Please choose a colour for your car")
End If
Console.ReadLine()
End Sub
End Module
Output:
----------------------------------------------------------------------
Activity 4.3
Program:
Imports System
Module Program
Sub Main()
Dim level As Integer
Dim salary As Integer
Dim paygrade As Integer
If (paygrade = 7) Then
Console.WriteLine("salary = 1.04")
ElseIf (level >= 0 & level <= 8) Then
Console.WriteLine("salary = 1.05")
Else
'if none of the above conditions is satisfied, execure the followiing
statement
End If
Console.WriteLine("Salary does not match with the above conditions")
Console.WriteLine("Salary is ", salary)
Console.WriteLine("Hit any key to exit...")
Console.ReadLine()
End Sub
End Module
Output:
salary = 1.05
Salary does not match with the above conditions
Salary is
Hit any key to exit...
------------------------------------------------------------------------
Activity 4.4
Program:
Imports System
Module Program
Sub Main()
Dim Number As Integer
Select Case (Number)
Case "1", "10"
Console.WriteLine("Range = 1")
Case "9"
Console.WriteLine("Range = 2")
Case "2"
Console.WriteLine("Range = 3")
Case "3", "5"
Console.WriteLine("Range = 5")
Console.WriteLine("Range = 6")
Console.ReadLine()
End Select
End Sub
End Module
-----------------------------------------------------------------------
Activity 5.1
Program:
Imports System
Module Program
Sub Main()
' Initialization and declaration of variable i
Dim i As Integer = 1
Do
' Executes the following statement
Console.WriteLine("Value of variable I is: {0}", i)
i = i + 1 'increment the variable i by 1
Loop While i < 10 ' Define the while condition
Output:
--------------------------------------------------------------------
Activity 5.2
Imports System
Module Do_loop
Sub Main()
' Initialization and declaration of variable i
Dim i As Integer = 1
Do
' Executes the following statement
Console.WriteLine("Value of variable I is: {0}", i)
i = i + 1 'increment the variable i by 1
Loop Until i = 10 ' Define the Until condition
Output:
---------------------------------------------------------------------
Activity 5.3
Program:
Imports System
Module Table
Sub Main()
' declaration of i and num variable
Dim i, num As Integer
Console.WriteLine("Enter any number to print the table: ")
num = Console.ReadLine() ' accept a number from the user
Console.WriteLine(" Table of " & num)
'define for loop condition, it automatically initialize step to 1
For i = 1 To 10
Console.WriteLine(" " & num & " * " & i & " = " & i * num)
Next
Console.ReadKey()
End Sub
End Module
Output:
---------------------------------------------------------------------
Activity 5.4
Program:
Imports System
Module Nested_loop
Sub Main()
Dim i, j As Integer
For i = 1 To 3
'outer loop
Console.WriteLine(" Outer loop, i = {0}", i)
'Console.Writeline(vbCrLf)
'inner loop
For j = 1 To 4
Console.WriteLine(" Inner loop, j = {0}", j)
Next
Console.WriteLine()
Next
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Output:
Outer loop, i = 1
Inner loop, j = 1
Inner loop, j = 2
Inner loop, j = 3
Inner loop, j = 4
Outer loop, i = 2
Inner loop, j = 1
Inner loop, j = 2
Inner loop, j = 3
Inner loop, j = 4
Outer loop, i = 3
Inner loop, j = 1
Inner loop, j = 2
Inner loop, j = 3
Inner loop, j = 4
--------------------------------------------------------------------
Activity 5.5
Program:
Imports System
Module Program
Sub Main()
'declare and inititalize an array as integer
Dim An_array() As Integer = {1, 2, 3}
Dim i As Integer 'declare i as integer
For Each i In An_array
Console.WriteLine(" Value of i is {0}", i)
Next
Console.WriteLine("Press any key to exit...")
Console.ReadLine()
End Sub
End Module
Output:
Value of i is 1
Value of i is 2
Value of i is 3
Press any key to exit...
-----------------------------------------------------------------
Activity 5.6
Program:
Imports System
Module Program
Sub Main()
'declare an integer variable
Dim n, remainder, sum As Integer
sum = 0
Console.WriteLine(" Enter the number: ")
n = Console.ReadLine() ' Accept a number from the user
End Sub
End Module
Output:
-----------------------------------------------------------------
Activity 5.7
Program:
Imports System
Module Program
Sub Main()
'declare i and j as integer variable
Dim i As Integer = 1
While i < 4
'Outer loop statement
Console.WriteLine(" Counter value of Outer loop is {0}", i)
Dim j As Integer = 1
While j < 3
'Inner loop statement
Console.WriteLine(" Counter value of Inner loop is {0}", j)
j = j + 1 ' Increment Inner Counter variable by 1
End While
Console.WriteLine() 'print space
i = i + 1 ' Increment Outer Counter variable byy 1
End While
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Output:
-----------------------------------------------------------------------
Program:
Imports System
Module Program
Sub Main()
For i As Integer = 1 To 4 'Outer loop
Console.WriteLine(" Table of " & i)
For j As Integer = 1 To 12 'Inner loop
Console.WriteLine(" " & i & " * " & j & " = " & i * j)
Next
Console.WriteLine()
Next
End Sub
End Module
Output:
Table of 1
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
1 * 11 = 11
1 * 12 = 12
Table of 2
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 11 = 22
2 * 12 = 24
Table of 3
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
Table of 4
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
4 * 11 = 44
4 * 12 = 48
-----------------------------------------------------------------------
Program:
Imports System
Module Program
Sub Main()
Dim i As Integer
For i = 1 To 5 'Outer loop
Console.WriteLine(i)
Next
End Sub
End Module
Output:
1
2
3
4
5
------------------------------------------------------------------------
Program:
Imports System
Module Program
Sub Main()
Dim i As Integer = 1
While (i < 8) 'checks while condition
Console.WriteLine(i) 'prints i to screen
i += 1 'increments i by 1
End While
End Sub
End Module
Output:
1
2
3
4
5
6
7
-------------------------------------------------------------------------
Program:
Imports System
Module Program
Sub Main()
Dim a, b, c As Integer
a = 500
b = 350
c = 200
Output:
----------------------------------------------------------------------------
Program:
Imports System
Module Program
Sub Main()
Dim num As Integer
Console.Write("Enter a number: ")
num = Console.ReadLine()
Console.WriteLine()
If (num Mod 2) = 0 Then
Console.WriteLine("{0} is an even number.", num)
Else
Console.WriteLine("{0} is an odd number.", num)
End If
End Sub
End Module
Output:
-----------------------------------------------------------------------------
Program:
Module Program
Sub Main()
Dim num1 As Integer = 45
Dim num2 As Integer = 65
If num1 > num2 Then
Console.WriteLine("{0} is greater, and {1} is lesser.", num1, num2)
ElseIf num2 > num1 Then
Console.WriteLine("{0} is greater, and {1} is lesser.", num2, num1)
Else
Console.WriteLine("{0} is equal to {1}", num1, num2)
End If
End Sub
End Module
Output:
---------------------------------------------------------------------------
Program:
Module Program
Function grade_score(ByVal score As Integer) As Char
If score >= 70 Then
Return "A"
ElseIf score >= 60 And score < 70 Then
Return "B"
ElseIf score >= 50 And score < 60 Then
Return "C"
ElseIf score >= 45 And score < 50 Then
Return "D"
ElseIf score >= 40 And score < 45 Then
Return "E"
Else
Return "F"
End If
End Function
Sub Main()
Dim subject(2) As String
Dim score(2) As Integer
For i As Integer = 0 To 2
Console.WriteLine("Enter Subject {0}: ", i + 1)
subject(i) = Console.ReadLine()
Console.WriteLine("Enter score for {0}: ", subject(i))
score(i) = Integer.Parse(Console.ReadLine())
Console.WriteLine()
Next
For j As Integer = 0 To 2
Console.WriteLine("Subject: {0}", subject(j))
Console.WriteLine("Score: {0}", score(j))
Console.WriteLine("Grade: {0}", grade_score(score(j)))
Console.WriteLine()
Next
End Sub
End Module
Output:
---------------------------------------------------------------------------
Program:
Module Program
Sub Main()
Dim num As Integer
For num = 1 To 7
Select Case (num)
Case "1"
Console.WriteLine("Day {0} of the week is Sunday.", num)
Case "2"
Console.WriteLine("Day {0} of the week is Monday.", num)
Case "3"
Console.WriteLine("Day {0} of the week is Tuesday.", num)
Case "4"
Console.WriteLine("Day {0} of the week is Wednesday.", num)
Case "5"
Console.WriteLine("Day {0} of the week is Thursday.", num)
Case "6"
Console.WriteLine("Day {0} of the week is Friday.", num)
Case "7"
Console.WriteLine("Day {0} of the week is Saturday.", num)
End Select
Next
End Sub
End Module
Output:
----------------------------------------------------------------------------
Activity 6.1
Program:
Imports System
Module Program
Sub Main()
Dim whole_numbers(19) As Integer
whole_numbers(0) = 1
For k As Integer = 0 To 19
whole_numbers(k) = k + 1
Console.Write("{0}", whole_numbers(k))
Next
End Sub
End Module
Output:
1234567891011121314151617181920
------------------------------------------------------------------------------
Activity 6.2
Program:
Module Program
Sub Main()
Dim intRanNum(19) As Integer
Dim inty As Integer
Randomize()
' load the "intRanNum" array with
' random numbers between 1 and 200...
For inty = 0 To 19
intRanNum(inty) = Int(200 * Rnd() + 1)
Next
Console.WriteLine("Twenty random numbers between 1 and 200: ")
' display the contents of the "intRanNum" array...
For inty = 0 To 19
Console.WriteLine(CStr(intRanNum(inty)))
Next
Console.WriteLine("Hit the Enter button to exit window.")
Console.ReadLine()
End Sub
End Module
Output:
----------------------------------------------------------------------------
Activity 6.3
Imports System
Module Program
Sub Main()
Dim sales(3, 5) As Decimal
---------------------------------------------------------------------------
Activity 6.4
Program:
Imports System
Module Program
Sub Main()
Dim intSpeed() As Integer
Dim intEnterNum As Integer
Dim intA As Integer
Dim totSpeed As Integer
Dim avgSpeed As Decimal
ReDim intSpeed(intEnterNum - 1)
Output:
Speed #1: 50
Speed #2: 22.5
Speed #3: 55.9
-----------------------------------------------------------------------
Activity 6.5
Program:
Imports System
Module Program
Sub Main()
Dim okekes() = {"Ekene", "Nkem", "Chinwe", "Duru", "Chidi", "Nneka"}
Array.Sort(okekes)
Array.Reverse(okekes)
For Each s As String In okekes
Console.Write("{0} ", s)
Next
Console.ReadKey()
End Sub
End Module
Output:
----------------------------------------------------------------------
Activity 6.6
Program (Edited):
Imports System
Module Program
Sub main()
Dim counter As Integer = 0
For i = 1 To 500
If i Mod 4 = 0 Then
counter = counter + 1
End If
Next
Console.WriteLine("There are {0} factors of 4 inbetween 1 and 500.",
counter)
End Sub
End Module
Output:
-----------------------------------------------------------------------
Activity 6.7
Program:
Imports System
Module Program
Sub main()
Dim arrs() As Integer = {2, 32, 14, 11, 25, 11, 38, 50, 34, 15, 44, 79}
Dim arr_index As Integer
arr_index = seqSearch(arrs, 1, arrs.Length)
If (arr_index <> -1) Then
Console.WriteLine("The item was found at the position {0}.", arr_index)
End If
Console.ReadLine()
End Sub
End Module
------------------------------------------------------------------------
Program:
Imports System
Module Program
Sub main()
Dim friends(15) As String '{"Nedu", "Ayobami", "Nneka", "Ubong", "Chidi",
"Infinity", "Gospel", "Akam-Obong", "Newton", "Blessing"}
friends(0) = "Nedu"
friends(1) = "Ayobami"
friends(2) = "Nneka"
friends(3) = "Ubong"
friends(4) = "Chidi"
friends(5) = "Infinity"
friends(6) = "Gospel"
friends(7) = "Akam-Obong"
friends(8) = "Newton"
friends(9) = "Blessing"
For i As Integer = 0 To 9
Console.WriteLine(friends(i))
Next
Console.WriteLine()
End Sub
End Module
Output:
Nedu
Ayobami
Nneka
Ubong
Chidi
Infinity
Gospel
Akam-Obong
Newton
Blessing
------------------------------------------------------------------------
Program:
Imports System
Module Program
Sub main()
Dim arr_num() As Integer
arr_num = New Integer() {2, 4, 6, 8, 10} 'initializes "arr_name" array
Dim k As Integer
For k = 0 To 4 'output arr_num array
Console.Write("{0}", arr_num(k) * arr_num(k) & vbTab)
Next
Console.ReadLine()
End Sub
End Module
Output:
4 16 36 64 100
------------------------------------------------------------------------
Practice 6.3
Program:
Imports System
Module Program
Sub main()
Dim arr_num() As Integer
Dim num As Integer
Console.Write("How many numbers: ")
num = Console.ReadLine() ' collect the number of numbers to check from the
user
Console.WriteLine()
' collects the different number elements from user and stores in the
"arr_num" array
For i As Integer = 0 To (num - 1)
Console.WriteLine("Enter number: ")
arr_num(i) = Console.ReadLine()
Next
Output:
Enter number:
67
Enter number:
80
Enter number:
12
--------------------------------------------------------------------------
Program:
Module Program
Sub main()
Dim ran_num(6) As Integer
Console.WriteLine("Enter 7 random numbers: ")
For i As Integer = 0 To 6
ran_num(i) = Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine()
Console.Write("Sorted List: ")
For j As Integer = 0 To 6
Console.Write(ran_num(j) & " ")
Next
End Sub
End Module
Output:
Sorted List: 2 23 32 34 45 67 86
-------------------------------------------------------------------------
Module Program
Sub main()
Console.Write("Enter a number: ")
Dim num As Integer = Convert.ToInt32(Console.ReadLine())
Dim factorial As Integer = 1
For i As Integer = 1 To num
factorial *= i
Next
Console.WriteLine("{0}! is: {1}.", num, factorial)
Console.WriteLine()
End Sub
End Module
Output:
Enter a number: 6
6! is: 720.
----------------------------------------------------------------------
LAB 7
Program:
Imports Microsoft.VisualBasic
Q1
----------------------------
Q2
----------------------------
Q3
----------------------------
Q4
If this program is ran, and 14 (for example) is inputted, the expected output is
14.00.
The output is gotten from calling the Format Function, with "dblTestNumber" as the
first argument, our preffered format style (in our case is "##, ##0.00")as second
argument. The Format function returns a string formmatted according to instruction
contained in a format String espression. The specified format in the code above the
format specified, adds 2 decimal places to the number passed into the Format
function.
----------------------------
Q5
Procedure and functions are used for condensing repeated operatins such as the
frequently used calculations, text and control manipulation. They are imporatant
for breaking a program into discrete logocal limits, thereby making debugging and
code maintenance easier and also allow us to resuse those code blocks again in
other programs.
----------------------------
Q6
False. Sub procedures don't return any values.
----------------------------
Q7
"&" is not a logical operator.
----------------------------
Q8
"If else and select case"
----------------------------
Q9
Dim [Variable_Name] As [defined Data type] = [Value]
-----------------------------------------------------------------------------
Activity 8.1
Program:
Observation:
A window popped up, containg a text box with the text "Hello. Welcome to University
of Port Harcourt". The text can be edited.
------------------------------------------------------------------------------
Activity 8.2
Upon running the code, a form window appeared with two textbox to collect username
and password from the user, and a login button. The characters typed into the
password box was masked by the asterisk character (*) to protect the password typed
in from being exposed.
------------------------------------------------------------------------------
Activity 8.3
-----------------------------------------------------------------------------
Practice Exercise 8.1
Control properties are attributes that define the appearance and behavior of a
control on a form.
Five most used form properties are:
1. Name: The Name property is used to give a unique identifier to a form or
control.
2. Text: The Text property is used to set the text that appears in the title bar of
a form.
3. BackColor: The BackColor property is used to set the background color of a form
or control.
4. ForeColor: The ForeColor property is used to set the text color of a form or
control.
5. Size: The Size property is used to set the width and height of a form or
control.
-----------------------------------------------------------------------------
Control methods are predefined actions that can be called to manipulate controls.
Examples of control methods include Hide(), Show(), Focus() e.t.c. While Control
events are predefined occurrences that can be responded to with event handlers in
the code. Examples of control events include Click, MouseHover, KeyPress,
TextChanged, etc.
-----------------------------------------------------------------------------
Practice Exercise 9.1
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Web.config |
Machine.config
→ Specific to a web application. → Applies to all .NET
applications on the machine
→ Configurations settings apply only to the current web application. →
Configuration settings are global and apply to all web applications.
→ Located in the root directory of the web application. → Located
in the %runtime%\config directory.
→ overrides setting in the Machine.config file. → Sets default
settings for all web applications.
→ Used to store application specific settings, such as: → Used to store
machine-wide settings, such as:
- Database connection strings - .NET Framework
settings
- Authentication and authorization settings. - Security
settings.
- Error handling and debugging settings. - Database
connection settings.
- Custom application settings. - Default
web application settings.
------------------------------------------------------------------------------