w3resource

C#: Separate odd and even integers in separate arrays

C# Sharp Array: Exercise-10 with Solution

Write a program in C# Sharp to separate odd and even integers into separate arrays.

Sample Solution:-

Calculating a Even Numbers:

Calculating a Even Numbers

Even Numbers between 1 to 100:

Even Numbers between 1 to 100

Calculating a Odd Numbers:

Calculating a Odd Numbers

Odd Numbers between 1 to 100:

Odd Numbers between 1 to 100

C# Sharp Code:

using System;

public class Exercise10 
{  
    public static void Main() 
    {
        // Declare arrays to hold integers
	    int[] arr1 = new int[10];  // First array to store user input
	    int[] arr2 = new int[10];  // Array to store even integers
	    int[] arr3 = new int[10];  // Array to store odd integers
        int i, j = 0, k = 0, n;    // Declare variables for counting and user input

        // Display a message prompting user to enter the number of elements to be stored in the array
        Console.Write("\n\nSeparate odd and even integers in separate arrays:\n");
        Console.Write("------------------------------------------------------\n");	

        Console.Write("Input the number of elements to be stored in the array :");
        n = Convert.ToInt32(Console.ReadLine()); // Read the number of elements entered by the user

        // Prompt the user to input 'n' elements in the array
        Console.Write("Input {0} elements in the array :\n", n);
        for (i = 0; i < n; i++)
        {
            Console.Write("element - {0} : ", i);
            arr1[i] = Convert.ToInt32(Console.ReadLine()); // Store user input in the first array
        }

        // Iterate through the first array to separate even and odd numbers into separate arrays
        for (i = 0; i < n; i++)
        {
            if (arr1[i] % 2 == 0) // Check if the number is even
            {
                arr2[j] = arr1[i]; // Store even number in the second array
                j++; // Increment the counter for the even array
            }
            else
            {
                arr3[k] = arr1[i]; // Store odd number in the third array
                k++; // Increment the counter for the odd array
            }
        }

        // Display even elements stored in the second array
        Console.Write("\nThe Even elements are : \n");
        for (i = 0; i < j; i++)
        {
            Console.Write("{0} ", arr2[i]); // Print even numbers
        }

        // Display odd elements stored in the third array
        Console.Write("\nThe Odd elements are :\n");
        for (i = 0; i < k; i++)
        {
            Console.Write("{0} ", arr3[i]); // Print odd numbers
        }
        Console.Write("\n\n"); // Print a new line for formatting
    }	
}
 
 

Sample Output:

                                                                                                       
Separate odd and even integers in separate arrays:                                                            
------------------------------------------------------                                                        
Input the number of elements to be stored in the array :2                                                     
Input 2 elements in the array :                                                                               
element - 0 : 3                                                                                               
element - 1 : 6                                                                                                
The Even elements are :                                                                                       
6                                                                                                       
The Odd elements are :                                                                                        
3

Flowchart:

Flowchart: Separate odd and even integers in separate arrays

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find maximum and minimum element in an array.
Next: Write a program in C# Sharp to sort elements of array in ascending order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/array/csharp-array-exercise-10.php