# Program to Count Number of Even and Odd numbers in Array in C language

## Program Description:

Write a program to count the Number of Even and Odd numbers in Array in C programming language. The program takes an input array from the user and counts the number of even numbers and odd numbers in the given array.

Input:

Output:

### Program Prerequisites:

It is recommended to know the basic of the Arrays and functions and how to pass arrays to the functions in C language. Please go through the following articles to learn more about these concepts.

## Number of Even and Odd numbers in Array Explanation:

1. Declare an array called numbers with a size of 100. Also declare the variables size and i.
2. Create two variables to track the count of the total number of even numbers and odd numbers. So Initialize the nEven and nOdd variables with Zero(0).
3. Take the desired size of the array from the user and update the size variable.
4. Now, We need to prompt the user to provide the values for the array.
• We are using a for loop to iterate over the array and prompt the user to provide the number.
• Read the number using scanf function and update the numbers[i] element of the array.
5. Once we got the elements from the user. The next step is to count the number of Even numbers and Odd numbers in an array.
6. To calculate the count of Even and Odd numbers, We need to traverse through the array and check each element.
• We use another For loop to traverse the array. At each iteration,
• Check if the numbers[i] is perfectly divisible by the 2. If the numbers[i] is perfectly divisible, Then the numbers[i] is an Even Number. So Increment the nEven variable by 1.
• Else then the number is an Odd Number, So increment the nOdd variable by 1.
7. Once the above loop is completed, The variables nEven and nOdd contains the total number of even numbers and odd numbers in the numbers array respectively.
8. Print the result on the standard output using printf function.

## C Program to count Even and Odd numbers in Array Algorithm:

1. Start the program
2. Declare numbers[100], i, and size variables.
3. Initialize nEven and nOdd with Zero.
4. Take array size from user
• scanf(“%d”, &size);
5. Update array elements
• Loop from zero(0) to size-1 using for loop
• update numbers[i] element – scanf(“%d”, &numbers[i]);
6. Traverse the Array using another for loop and check the each element
• For Loop from Zero(0) to size-1
• check if(numbers[i]%2 == 0) is true, Then increment nEven
• Else increment nOdd
7. Display results on the console.

## Program to find Number of Even and Odd numbers in Array in C:

Here is the program to count the number of even numbers and odd numbers in an array using C programming language

### Program Output:

Compile the program using GCC compiler (Any compiler)

gcc even-odd.c

Run the program.

Test 1:

Test 2:

The program is providing the desired results.

## Number of Even and Odd numbers in Array using user-defined functions:

In the above program, We used only one function to write the code. Let’s divide the above program and create a few more functions to do a specific task like reading a number, etc. By using the functions, We can reuse the code and the program becomes modular and easily write, and debug.

📢 Advantages of Functions

Let’s rewrite the above number of even and odd number programs and create a few functions like getNOdd and getNEven

Here we have defined three user-defined functions(except the main() function).

1. The read() function – Used to read the user input and update the given array
• Prototype of the read function –  void read(<strong>int</strong> numbers[], <strong>int</strong> size)
• read() function takes two formal arguments which are  numbers[] array and its  size.
• The read() function is used to take the input numbers from the user and update the  numbers array.
2. The  getNEven() function
• Prototype of the  getNEven() function –  int getNEven(int numbers[], int size)
• The  getNEven() function also takes two formal arguments, which are  numbers array and its size and returns an integer.
• The getNEven() function calculates the total number of even numbers in the numbers array and returns the results.
3. The  getNOdd() Function.
• Here is the prototype of the  getNOdd() function –  int getNOdd(int numbers[], int size)
• The getNOdd function takes two formal arguments which are numbers array and size variable.
• The getNOdd function calculates the total number of Odd numbers in the numbers array and returns the results back tot eh caller.

### Program Output:

Let’s compile and run the program.

As we can see from the above output, the Program is generating the expected results.

## Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 1 Response

1. […] C Program to Count Even and Odd numbers in Array […]