Write a program in C# to find the summation of negative numbers from a collection of numbers

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1042
Given an collection as -8, -7, 6, 0, 7, -1, 6. The objective is to find the summation of negative numbers from the collection of 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 }
.OrderBy(o => o)
.TakeWhile(t => t < 0)
.Sum());
Console.ReadKey();
}

}
}

Output: -16

Comments or Responses

Login to post response