Basic Programming C# Examples

C# Program to Count Even Number of Elements in Given Array

The Program in C# Program to Count Even Number of Elements in Given Array is given below:

using System;

namespace EvenNumberCounter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array to check number of even numbers:");
            int numberOfElements = Convert.ToInt32(Console.ReadLine());
            int[] numbers = new int[numberOfElements];
            int evenCount = 0;

            for (int i = 0; i < numberOfElements; i++)
            {
                Console.WriteLine("Okay, enter element " + (i + 1) + ":");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }

            foreach (int number in numbers)
            {
                if (number % 2 == 0)
                {
                    evenCount++;
                }
            }

            Console.WriteLine("Well, the number of even numbers is: " + evenCount);
        }
    }
}

Output:

Hello Codeauri Family, enter the number of elements in the array to check number of even numbers:
2

Okay, enter element 1:
90
Okay, enter element 2:
10
Well, the number of even numbers is: 2

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; is a directive that includes the System namespace, which contains several important classes, including the Console class used later in the code.
  2. The next line namespace EvenNumberCounter declares the namespace for the code, which helps organize it and avoid naming collisions with other code.
  3. The next line class Program declares the main class for the code, which contains the Main method that will be executed when the program is run.
  4. The next line static void Main(string[] args) declares the Main method, which is the entry point for the program. This method takes an array of strings as an argument, which can be used to pass command-line arguments to the program.
  5. The next line Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array to check number of even numbers:"); outputs a prompt to the console asking the user to enter the number of elements in the array.
  6. The next line int numberOfElements = Convert.ToInt32(Console.ReadLine()); reads the user’s input as a string, converts it to an integer using the Convert.ToInt32 method, and stores the result in the numberOfElements variable.
  7. The next line int[] numbers = new int[numberOfElements]; creates an array of integers with numberOfElements elements, which will be used to store the user’s input.
  8. The next line int evenCount = 0; declares and initializes a variable evenCount to keep track of the number of even numbers in the array.
  9. The next line for (int i = 0; i < numberOfElements; i++) starts a for loop that will iterate numberOfElements times.
  10. The next line Console.WriteLine("Okay, enter element " + (i + 1) + ":"); outputs a prompt to the console asking the user to enter the next element of the array.
  11. The next line numbers[i] = Convert.ToInt32(Console.ReadLine()); reads the user’s input as a string, converts it to an integer using the Convert.ToInt32 method, and stores the result in the i-th element of the numbers array.
  12. The next line foreach (int number in numbers) starts a foreach loop that will iterate through each element of the numbers array.
  13. The next line if (number % 2 == 0) checks if the current element of the array, number, is even by checking if its remainder when divided by 2 is equal to 0.
  14. The next line evenCount++; increments the evenCount variable by 1 if the current element of the array is even.
  15. The next line Console.WriteLine("Well, the number of even numbers is: " + evenCount); outputs the final result, the number of even numbers in the array, to the console.

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