

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of even and odd elements in an array in C++
In this tutorial, we will be discussing a program to find the number of even and odd elements in an array.
For this we will be provided with an array. Our task is to calculate the number of even and odd elements in the given array.
Example
#include<iostream> using namespace std; void CountingEvenOdd(int arr[], int arr_size){ int even_count = 0; int odd_count = 0; //looping through the elements for(int i = 0 ; i < arr_size ; i++) { //checking if the number is odd if (arr[i]%2 != 0) odd_count ++ ; else even_count ++ ; } cout << "Number of even elements = " << even_count << "\nNumber of odd elements = " << odd_count ; } int main(){ int arr[] = {2, 3, 4, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); CountingEvenOdd(arr, n); }
Output
Number of even elements = 3 Number of odd elements = 2
- Related Questions & Answers
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Count subarrays with same even and odd elements in C++
- Count even and odd digits in an Integer in C++
- Find elements of an Array which are Odd and Even using STL in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Java program to Print Odd and Even Number from an Array
- Count odd and even digits in a number in PL/SQL
- Count rotations of N which are Odd and Even in C++
- Odd even sort in an array - JavaScript
- Count number of elements in an array with MongoDB?
- Swap Even Index Elements And Odd Index Elements in Python
- Sorting odd and even elements separately JavaScript
- Count and Sum of composite elements in an array in C++
Advertisements