ADVERTISEMENT
ADVERTISEMENT

C# program to find the index of even numbers using Linq

Here, we are going to learn how to find the index of even numbers using Linq in C#?
Submitted by Nidhi, on August 28, 2020

Here we will find the index of even numbers using Linq Select() and Where() methods.

Program:

The source code to find the index of even numbers using Linq is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the index of even numbers using Linq.
using System;
using System.Collections.Generic;
using System.Linq;

class Demo
{
    static void Main(string[] args)
    {
        List<int> intnums = new List<int>();

        intnums.Add(10);
        intnums.Add(11);
        intnums.Add(12);
        intnums.Add(13);
        intnums.Add(14);

        var IndexOfEvenNumber = intnums.Select((num, index) => new
                                {
                                    Numbers = num,
                                    IndexPosition = index
                                }).Where(n => n.Numbers % 2 == 0)
                                .Select(result => new
                                {
                                    Number = result.Numbers,
                                    IndexPosition = result.IndexPosition
                                });

        foreach (var value in IndexOfEvenNumber)
        {
            Console.WriteLine("Index:" + value.IndexPosition + " of Number: " + value.Number);
        }
    }
}

Output:

Index:0 of Number: 10
Index:2 of Number: 12
Index:4 of Number: 14
Press any key to continue . . .

Explanation:

In the above program, we created a list of integers. Then add the items to the List using Add() method.

var IndexOfEvenNumber = intnums.Select((num, index) => new
                        {
                            Numbers = num,
                            IndexPosition = index
                        }).Where(n => n.Numbers % 2 == 0)
                        .Select(result => new
                        {
                            Number = result.Numbers,
                            IndexPosition = result.IndexPosition
                        });

In the above method, we find the index with selected even number and assigned the result to the variable "IndexOfEvenNumber".

foreach (var value in IndexOfEvenNumber)
{
    Console.WriteLine("Index:" + value.IndexPosition + " of Number: " + value.Number);
}

In the above code, we print the index with selected even numbers on the console screen.

C# LINQ Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.