Program to find the second largest even number in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 241
Let's say we have an array of integers like {78,92,44,63,71,97}. The objective is to find the second largest even number. Below program will do so

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

var intCollection = new List<int>() { 78, 92, 44, 63, 71, 97 };
var secondLargetEven = intCollection.Where(i => i % 2 == 0).OrderByDescending(o => o).Skip(1).Take(1).First();
Console.WriteLine("Second largest Even Number is {0}", secondLargetEven);
Console.ReadKey();
}
}
}


Result
---------
Second largest Even Number is 78

Comments or Responses

Login to post response