Questions:
1. Create a VB.NET Console Application that defines a
function Smallest and calls this function from the main program.
The function Smallest takes three parameters, all of the Integer
data type, and returns the value of the smallest among the three
parameters. The main program should
(1) Prompt a message (using Console.WriteLine) to ask the user to
input three integers.
(2) Call the built-in function Console.ReadLine() three times to
get the user’s input.
(3) Convert the user’s input from strings to integers.
(4) Call the function Smallest, with the user’s input as its
parameters.
(5) Display the result of the function Smallest in the Console
window using Console.WriteLine.
You may assume that the user always correctly inputs integers from
the keyboard, and your program does not have to check whether the
user inputs something other than integers. Print your program and
handwrite the user’s input and output at the end of the printout of
your program.
2. Create a VB.NET Console Application that does the
following:
(1) Create an Integer array which contains 10 elements.
(2) Assign the following values to the elements in the array:
9, 1, 8, 4, 2, 6, 7, 3, 5, 10
(3) Use the For-Each-loop to compute and printout the sum of all
elements of the array in the Console window.
1.SmallestOfThree.vb
Module SmallestOfThree
'function that takes three parameters and returns the smallest of
the three integers
Function Smallest(ByVal a As Integer, ByVal b As Integer, ByVal c
As Integer) As Integer
'declare a variable that hold the smallest number
Dim min As Integer
'check if a is less than b and c
If a < b And a < c Then
'if yes then set a to min
min = a
'check if b is less than a and c
ElseIf b < a And b < c Then
'if yes then set b to min
min = b
'check if c is less than a and b
ElseIf c < a And c < b Then
'if yes then set c to min
min = c
End If
'return min
Smallest = min
End Function
Sub Main()
'declare thre variables to hold integers
Dim a, b, c As Integer
'prompt a message to the user
Console.Write("Enter three integers: ")
'read three integers from the user
a = Convert.ToInt32(Console.ReadLine())
b = Convert.ToInt32(Console.ReadLine())
c = Convert.ToInt32(Console.ReadLine())
Dim min As Integer
'by the smallest number by callinf the Smallest function by passing
a, b and c as parameters
min = Smallest(a, b, c)
'display the result to the user
Console.WriteLine(vbNewLine + "The smallest of the given numbers
is: " + min.ToString())
Console.ReadKey()
End Sub
End Module
Output
2.Sum of Elements in Array
Module Array
Sub Main()
'declare an array of 10 elements
Dim myArray(10) As Integer
'assign values to the elements in the array
myArray = {9, 1, 8, 4, 2, 6, 7, 3, 5, 10}
'declare a variable to hold the sum
Dim sum As Integer = 0
'using a for each loop get the sum of the elements in the
array
For Each ele As Integer In myArray
sum = sum + ele 'add ele to sum
Next
'display the sum to the console
Console.WriteLine("The sum of the elements in the array is: " +
sum.ToString())
Console.ReadKey()
End Sub
End Module
Output:
Get Answers For Free
Most questions answered within 1 hours.