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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Number_of_even_and_odd_terms_in_an_array {
- class Program {
- static void Main(string[] args) {
- int i, n, even = 0, odd = 0;
- Console.WriteLine("Enter the number of elements to be inserted: ");
- n = Convert.ToInt32(Console.ReadLine());
- int[] a = new int[n];
- Console.WriteLine("Enter the array elements:");
- for (i = 0; i < n; i++) {
- a[i] = Convert.ToInt32(Console.ReadLine());
- }
- for (i = 0; i < n; i++) {
- if (a[i] % 2 == 0) {
- even = even;
- even++;
- } else {
- odd = odd;
- odd++;
- }
- }
- Console.WriteLine("Number of even terms are: " + even);
- Console.WriteLine("Number of odd terms are: " + odd);
- Console.ReadLine();
- }
- }
- }
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.