Write a program in VB.net to find the count of negative numbers

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 80
Given an collection as -8, -7, 6, 0, 7, 1, 6. The objective is to find the count of negative numbers. The below program will do so

Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication2
Class Program
Private Shared Sub Main(args As String())
Console.WriteLine(New List(Of Integer)() From { _
-8, _
-7, _
6, _
0, _
7, _
1, _
6 _
}.TakeWhile(Function(t) t < 0).Count())
Console.ReadKey()
End Sub

End Class
End Namespace


In this program, we are iterating over the collection till the condition (number < 0) inside the TakeWhile extension method becomes false. And then using the Count() function we received teh desired output which is 2 in this case.

Comments or Responses

Login to post response