Basic Programming C# Examples

C# Program to Separate Odd & Even Integers in Separate arrays

The Program in C# Program to Separate Odd & Even Integers in Separate arrays is given below:

using System;

namespace SeparateOddAndEven
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array here: ");
            int n = int.Parse(Console.ReadLine());

            int[] numbers = new int[n];

            Console.WriteLine("Enter the elements of the array: ");
            for (int i = 0; i < n; i++)
            {
                numbers[i] = int.Parse(Console.ReadLine());
            }

            int oddCount = 0;
            int evenCount = 0;

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] % 2 == 0)
                {
                    evenCount++;
                }
                else
                {
                    oddCount++;
                }
            }

            int[] oddNumbers = new int[oddCount];
            int[] evenNumbers = new int[evenCount];

            int oddIndex = 0;
            int evenIndex = 0;

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] % 2 == 0)
                {
                    evenNumbers[evenIndex] = numbers[i];
                    evenIndex++;
                }
                else
                {
                    oddNumbers[oddIndex] = numbers[i];
                    oddIndex++;
                }
            }

            Console.WriteLine("Okay,the odd numbers in the array are: ");
            for (int i = 0; i < oddNumbers.Length; i++)
            {
                Console.WriteLine(oddNumbers[i]);
            }

            Console.WriteLine("Okay, the even numbers in the array are: ");
            for (int i = 0; i < evenNumbers.Length; i++)
            {
                Console.WriteLine(evenNumbers[i]);
            }

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter the number of elements in the array here:
3
Enter the elements of the array:
131
567
990
Okay,the odd numbers in the array are:
131
567
Okay, the even numbers in the array are:
990

Pro-Tips💡

Here are the step by step execution of above program:

The program starts by using the “System” namespace.

The program defines a namespace called “SeparateOddAndEven”.

The program defines a class called “Program”.

The program defines the Main method, which is the entry point of the program.

The program asks the user to enter the number of elements in the array and reads the input as an integer.

The program declares an integer array called “numbers” with the size equal to the number entered by the user.

The program uses a for loop to ask the user to enter each element of the array and reads the input as integers.

The program declares two integer variables, “oddCount” and “evenCount”, and assigns them the value 0.

The program uses a for loop to iterate through the elements in the “numbers” array.

The program checks if the current element is even by checking if its modulo 2 is equal to 0. If it is, the program increments the value of “evenCount” by 1. If it is not, the program increments the value of “oddCount” by

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X