Find a Nth highest or lowest number in a given series of numbers using VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 169
Imports System.Collections.Generic

Namespace ConsoleApplication2
Class Program
Private Shared Sub Main(args As String())

Dim lstNumbers As New List(Of Double)() From { _
1, _
2.06, _
-36.4, _
-4, _
5.9, _
61.9, _
79.8, _
8, _
9, _
18.0, _
23.7 _
}
Dim minNum As Double = 0
Dim maxNum As Double = 0

minNum = lstNumbers(0)
maxNum = lstNumbers(0)
For i As Integer = 1 To lstNumbers.Count - 1
'logic for obtaining the Minimum Number
If minNum > lstNumbers(i) Then
minNum = lstNumbers(i)
End If

'logic for obtaining the Maximum Number
If maxNum < lstNumbers(i) Then
maxNum = lstNumbers(i)
End If
Next
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum)
Console.ReadKey(True)
End Sub
End Class
End Namespace


Explanation: First of all we have collected the data and put into a collection.Then we have assumed the first collection element as the Maximum and Minimum number and put in two variables minNum and maxNum. Then we have looped through from second element till the last element in the collection and compared each element with minNum and maxNum variables. If the values found to be greater/smaller, then those values are assigned.

Comments or Responses

Login to post response