C# program to split the Even and Odd integers into different arrays

In C#, you can split an array of integers into separate arrays for even and odd numbers by checking if each element is divisible by 2. This technique is useful for data organization and filtering operations.

How It Works

The modulo operator (%) is used to determine if a number is even or odd. When a number is divided by 2, if the remainder is 0, the number is even; otherwise, it's odd.

Even vs Odd Number Detection Even Numbers num % 2 == 0 Remainder is 0 Odd Numbers num % 2 != 0 Remainder is 1

Using Fixed-Size Arrays

This approach uses pre-allocated arrays to store even and odd numbers separately −

using System;

public class Program {
   public static void Main() {
      int[] arr1 = new int[] { 77, 34, 59, 42, 99 };
      int[] arr2 = new int[5];  // For even numbers
      int[] arr3 = new int[5];  // For odd numbers
      int i, j = 0, k = 0;

      // Split numbers into even and odd arrays
      for (i = 0; i < 5; i++) {
         if (arr1[i] % 2 == 0) {
            arr2[j] = arr1[i];
            j++;
         } else {
            arr3[k] = arr1[i];
            k++;
         }
      }

      // Display even numbers
      Console.WriteLine("Even numbers...");
      for (i = 0; i < j; i++) {
         Console.WriteLine(arr2[i]);
      }

      // Display odd numbers
      Console.WriteLine("Odd numbers...");
      for (i = 0; i < k; i++) {
         Console.WriteLine(arr3[i]);
      }
   }
}

The output of the above code is −

Even numbers...
34
42
Odd numbers...
77
59
99

Using Dynamic Lists

For more flexibility, you can use List<int> which automatically resizes as elements are added −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      int[] numbers = { 12, 15, 8, 21, 30, 7, 18 };
      List<int> evenNumbers = new List<int>();
      List<int> oddNumbers = new List<int>();

      // Split numbers using modulo operator
      foreach (int num in numbers) {
         if (num % 2 == 0) {
            evenNumbers.Add(num);
         } else {
            oddNumbers.Add(num);
         }
      }

      // Display results
      Console.WriteLine("Original array: " + string.Join(", ", numbers));
      Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers));
      Console.WriteLine("Odd numbers: " + string.Join(", ", oddNumbers));
   }
}

The output of the above code is −

Original array: 12, 15, 8, 21, 30, 7, 18
Even numbers: 12, 8, 30, 18
Odd numbers: 15, 21, 7

Using LINQ for Functional Approach

LINQ provides a more concise way to filter arrays using lambda expressions −

using System;
using System.Linq;

public class Program {
   public static void Main() {
      int[] numbers = { 45, 22, 13, 88, 91, 16 };

      // Split using LINQ
      int[] evenNumbers = numbers.Where(n => n % 2 == 0).ToArray();
      int[] oddNumbers = numbers.Where(n => n % 2 != 0).ToArray();

      Console.WriteLine("Original: " + string.Join(" ", numbers));
      Console.WriteLine("Even: " + string.Join(" ", evenNumbers));
      Console.WriteLine("Odd: " + string.Join(" ", oddNumbers));
      Console.WriteLine($"Even count: {evenNumbers.Length}, Odd count: {oddNumbers.Length}");
   }
}

The output of the above code is −

Original: 45 22 13 88 91 16
Even: 22 88 16
Odd: 45 13 91
Even count: 3, Odd count: 3

Comparison

Approach Memory Usage Performance Code Simplicity
Fixed Arrays Pre-allocated (may waste space) Fast More verbose
Dynamic Lists Dynamic (efficient) Good Moderate
LINQ Creates new arrays Good for small datasets Very concise

Conclusion

Splitting arrays into even and odd numbers can be accomplished using the modulo operator to check divisibility by 2. Choose fixed arrays for simple cases, Lists for dynamic sizing, or LINQ for functional programming approaches based on your specific requirements.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements