+92 332 4229 857 99ProjectIdeas@Gmail.com

Counting Even & Odd Elements In Array (C#.net)



The following code shows you how to count even and odd elements in an array. And displaying the counted numbers on console.


Code
public static void Main(string[] args)
{
     
     int[] arr = { 2, 3, 1, 5, 6, 4, 7, 8, 9, 11, 10 };
     int size = arr.Length;
    
     evenOddElements(arr,size);

}

public void evenOddElements(int[] arr, int size)
{

     int even = 0, odd = 0;

     for (int i = 0; i < size; i++)
     {
         if (arr[i] % 2 == 0)
         {
            even++;
         }
         else
         {
            odd++;
         }
     }

    Console.WriteLine("Even Array Elements Are : " + even);

    Console.WriteLine();

    Console.WriteLine("ODD Array Elements Are : " + odd);

}

Output of the program is:
Even Array Elements Are : 5
Odd Array Elements Are : 6

0 comments: