
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements