C++ Program to Count Odd and Even Numbers from Array

Write C++ Program to Count Odd and Even Numbers from Array

// CPP Program to Count Odd and Even Numbers from Array

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1, 2, 3, 4, 5, 6, 8, 21, 22, 33, 44, 66};
	int odd_count = 0, even_count = 0, i;

	int n = sizeof(A) / sizeof(A[0]);

        cout << "Array A :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";

	for(i = 0;i < n;i++)
	{
		if(A[i] % 2 == 0)
		   even_count += 1;
		else
		   odd_count += 1;
	}

	cout << "\n\nCount of Even Numbers :--> " << even_count;
	cout << "\nCount of Odd Numbers :--> " << odd_count;
	return 0;
}

Output:

Array A :--> 1  2  3  4  5  6  8  21  22  33  44  66

Count of Even Numbers :--> 7
Count of Odd Numbers :--> 5

≪ Previous | Next ≫