Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Counting Even and Odd

Counting Even and Odd Numbers


Video Summary: https://youtu.be/c5H29u-DMgM


In this exercise, you will count the number of even and odd integers in the given array:

 
#include <iostream>

using namespace std;

int main(){
    int numbers [] = {1, 5, 32, 8, 12, 3, 43, 29, 10, 42};
    int numEven = 0;
    int numOdd = 0;
    
    return 0;
}

Suggested steps:

  1. Create a for loop that will run for the size of the array. You can find this with sizeof(numbers)/sizeof(int) instead of counting them out yourself. You need to divide because sizeof will return the size in bytes, so to get the actual number you need to divide by the number of bytes in an integer
  2. Write an if statement that increments the counter of numEven if the number is even, and an else that increments numOdd if it is not

When you are finished, your output might look like this:

There are 5 even numbers and 5 odd numbers

Solution