Saturday, 9 February 2013

get even numbers and odd numbers from an array C#

get even numbers and odd numbers from an array C#

Gets even and odd arrays from an integer array.

Example 1: 

           Group array elements by even or odd. using condition i % 2==0.  i.,e there will a 2 groups
if condition is true number will be in Even group, if condition is not true number will be in Odd group.


 static void EVENODDGROUP()
        {
            int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;

            foreach (var va in query)
            {
                Console.WriteLine("key={0}",va.Key);

                foreach (var v in va)
                {
                    Console.Write(v); Console.Write("\t");
                }

            }
        }


output:
key=Even group
10      2       4       6       8
key=Odd Group
3       5       7       9       1


Example 1: Get Even Array

  If condition is true then using ToArray() method get even numbers
 
            int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;
            foreach (var va in query)
            {
                if (va.Key == true)
                {

                    int[] EvenArray = va.ToArray();
foreach (var v in EvenArray)
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
                }
            }

OUTPUT:10      2       4       6       8

Example 1: Get Odd Array

  If condition is true then using ToArray() method get odd numbers
           
int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;

            foreach (var va in query)
            {
                if (va.Key == false)                {
                    int[] OddArray = va.ToArray();
                    foreach (var v in OddArray)
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
                }
            }

           
        }
   
 
OUTPUT:3       5       7       9       1

Note:  User can sort Even or add arrays by using 

                    var EvenArray = va.OrderBy(v => v);
                    foreach (var v in EvenArray) //OddArray
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
 
Tags: Get Even odd arrays from an integer array. ToArray extension method, i%2==0,integer arrays, linq group by, linq order by,sort even arrays,sort odd arrays.