Number Of Odd And Even Terms In An Array Using C#.NET

An array is a type of data structure that contains a number of homogeneous data type elements under a same/common name. Array is used for storing a large amount of homogeneous data type. Array is used for making the searching and sorting techniques.

Now, we will see how to create a C# program that counts the number of odd terms and even terms in an array.

First, open Visual Studio and create a project of Console App.

Then, write the following code in the project.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Number_of_even_and_odd_terms_in_an_array {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             int i, n, even = 0, odd = 0;  
  10.             Console.WriteLine("Enter the number of elements to be inserted: ");  
  11.             n = Convert.ToInt32(Console.ReadLine());  
  12.             int[] a = new int[n];  
  13.             Console.WriteLine("Enter the array elements:");  
  14.             for (i = 0; i < n; i++) {  
  15.                 a[i] = Convert.ToInt32(Console.ReadLine());  
  16.             }  
  17.             for (i = 0; i < n; i++) {  
  18.                 if (a[i] % 2 == 0) {  
  19.                     even = even;  
  20.                     even++;  
  21.                 } else {  
  22.                     odd = odd;  
  23.                     odd++;  
  24.                 }  
  25.             }  
  26.             Console.WriteLine("Number of even terms are: " + even);  
  27.             Console.WriteLine("Number of odd terms are: " + odd);  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
The following screen will appear.



Run the application to see the output.



This is how we create a program that counts the number of odd terms and even terms in an array.
Ebook Download
View all
Learn
View all