👤
a fost răspuns

Given an array of integer numbers, compute how many values from the array are odd and how many of them are even.


Răspuns :

VlOd

#include <iostream>

#include <string>

#include <iterator>

using namespace std;

template<class T, size_t N>

constexpr size_t size(T(&)[N])

{

return N;

}

int main()

{

int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

int arraySize = size(array);

cout << "Even numbers: ";

for (int i=0; i < arraySize; i++)

{

 int number = array[i];

 if (number % 2 == 0)

 {

  if (i+2 >= arraySize)

   cout << to_string(number);

  else

   cout << to_string(number) + ",";

 }

}

cout << "\n";

 

cout << "Odd numbers: ";

for (int i=0; i < arraySize; i++)

{

 int number = array[i];

 if (number % 2 != 0)

 {

  if (i+2 >= arraySize)

   cout << to_string(number);

  else

   cout << to_string(number) + ",";

 }

}

cout << "\n";

 

return 0;

}

I hope this is helpful!