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:
- The first line
using System;
is a directive that includes theSystem
namespace, which contains several important classes, including theConsole
class used later in the code. - The next line
namespace EvenNumberCounter
declares the namespace for the code, which helps organize it and avoid naming collisions with other code. - The next line
class Program
declares the main class for the code, which contains theMain
method that will be executed when the program is run. - The next line
static void Main(string[] args)
declares theMain
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. - 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. - The next line
int numberOfElements = Convert.ToInt32(Console.ReadLine());
reads the user’s input as a string, converts it to an integer using theConvert.ToInt32
method, and stores the result in thenumberOfElements
variable. - The next line
int[] numbers = new int[numberOfElements];
creates an array of integers withnumberOfElements
elements, which will be used to store the user’s input. - The next line
int evenCount = 0;
declares and initializes a variableevenCount
to keep track of the number of even numbers in the array. - The next line
for (int i = 0; i < numberOfElements; i++)
starts afor
loop that will iteratenumberOfElements
times. - 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. - The next line
numbers[i] = Convert.ToInt32(Console.ReadLine());
reads the user’s input as a string, converts it to an integer using theConvert.ToInt32
method, and stores the result in thei
-th element of thenumbers
array. - The next line
foreach (int number in numbers)
starts aforeach
loop that will iterate through each element of thenumbers
array. - 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. - The next line
evenCount++;
increments theevenCount
variable by 1 if the current element of the array is even. - 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.