Program To Define And Initialize An Array And Display Even Elements Stored In It Using C/C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code


To copy code: Click  View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

An array is initialized with a set of values like this: int x[6]={10, 11, -5, 20, 15, 18}; .Here, we are initializing the integer array ‘x’ with 6 elements. These elements are assigned in order of their appearance i.e. 10 is assigned to the array index x[0], 11 is assigned to array index x[1] and so on.

1. In the above program we first define and initialize an array ‘x’ and then display the even numbers stored in it by using for loop.

2. Now, to display only the even elements, we check whether the given element is even or odd. We divide the elements by 2 (x[i]%2==0) and check the remainder. If the remainder comes out to be ‘0’ then the number is even which is then displayed on the output screen. Otherwise, the number is odd and it is not displayed.


Display even numbers

10 20 18