Write a program in C# to find the count of negative numbers

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 69
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

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new List<int>() { -8, -7, 6, 0, 7, 1, 6 }
.TakeWhile(t => t < 0)
.Count());
Console.ReadKey();
}

}
}


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