C++ Program to Print Even Numbers in an Array
Last Updated :
06 Feb, 2023
Given an array of numbers, the task is to print all the even elements of the array. Let us understand it with few examples mentioned below.
Example:
Input: num1 = [2,7,6,5,4]
Output: 2, 6, 4
Input: num2 = [1,4,7,8,3]
Output: 4, 8
1. Using for loop
Approach: Iterate each element in the given using for loop and check if num % 2 == 0 that means if the remainder when divided by 2 is 0 it is even else it is odd. If the condition is satisfied, then only print the number.
Below is the implementation of the above approach:
C++
// C++ program to print even numbers in an array
// using for loop
#include <iostream>
using namespace std;
int main()
{
// array of numbers
int num1[] = { 2, 7, 6, 5, 4 };
// size of an array
int n = 5;
for (int i = 0; i < n; i++) {
// to check the number is even or not
if (num1[i] % 2 == 0) {
cout << num1[i] << " ";
}
}
return 0;
}
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
2. Using while loop
Approach: Iterate each element in the given using while loop and check if num % 2 == 0 that means if the remainder when divided by 2 is 0 it is even else it is odd. If the condition is satisfied, then only print the number.
Below is the implementation of the above approach:
C++
// C++ program to print even numbers in an array
// using while loop
#include <iostream>
using namespace std;
int main()
{
// array of numbers
int num1[] = { 7, 5, 9, 6, 2, 3, 4 };
// size of an array
int n = 7;
int i = 0;
while (i < n) {
// to check the number even or not
if (num1[i] % 2 == 0) {
cout << num1[i] << " ";
}
i++;
}
return 0;
}
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used
3.Using Bitwise & operator
Approach: Iterate each element in the given array using for loop and check if num & 1 == 0 , as we know AND of 1 and num will give 0 , if num rightmost bit is not set and in even numbers rightmost bit isn't set. If the condition is satisfied, then only print the number.
Below is the implementation of the above approach:
C++
// C++ program to print all even numbers in the array
#include <iostream>
using namespace std;
int main() {
// array of numbers
int num1[] = { 2, 7, 6, 5, 4 };
// size of an array
int n = 5;
for (int i = 0; i < n; i++) {
int x=num1[i]&1; // x will store Bitwise AND of number and 1
// as we know , bitwise AND of even number and 1 is 0
// check if it is 0 , then print number
if (x==0) {
cout << num1[i] << " ";
}
}
return 0;
}
// This code is contributed by nikhilsainiofficial546
Time Complexity: O(N) , As we are iterating the whole array
Auxiliary Space: O(1), As constant extra space is used
4.Using Bitwise | operator
Approach: Iterate each element in the given array using for loop and check if num | 1 ==num+1, if the condition satisfies then it is even number as we know OR of 1 and num will give num+1 in the case of even numbers, then only print the number.
Below is the implementation of the above approach:
C++
// C++ program to print all even numbers in the array
#include <iostream>
using namespace std;
int main() {
// array of numbers
int num1[] = { 2, 7, 6, 5, 4 };
// size of an array
int n = 5;
for (int i = 0; i < n; i++) {
// as we know , bitwise OR of even number and 1 is even number+1
// check if it is even , then print number
if ((num1[i] | 1)==num1[i]+1) {
cout << num1[i] << " ";
}
}
return 0;
}
// This code is contributed by tvsk
Time Complexity: O(N) , As we are iterating the whole array
Auxiliary Space: O(1), As constant extra space is used
5. Using recursion
Approach: Pass the array to the recursive function, which will traverse till the last element and print the element if it is even.
Below is the implementation of above approach:
C++
// C++ program to print all even numbers in the array
#include <iostream>
using namespace std;
// Recursive function 'printEven' which takes:
// integer array 'arr'
// array size 'n'
// current index
void printEven(int* arr, int n, int index) {
// halting recursion when index exceeds size of array
if(index >= n) return;
// if current element & 1 results 0,
// and !(0) is 1
// thus, every even element will be printed
if(!(arr[index] & 1)) cout << arr[index] << " ";
// recursing to next index
printEven(arr, n, ++index);
}
int main() {
// array of numbers
int arr[] = { 2, 7, 6, 5, 4 };
// size of an array
int n = 5;
printEven(arr, n, 0);
return 0;
}
// This code is contributed by Om Mishra (om_mishra)
Time Complexity: O(N), as we are traversing entire array
Auxiliary Space: O(1), as constant extra space is used
Similar Reads
How to Print an Array in C++?
In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr
2 min read
Program for n-th even number
Given a number n, print the nth even number. The 1st even number is 2, 2nd is 4 and so on. Examples: Input : 3Output : 6First three even numbers are 2, 4, 6, .. Input : 5Output : 10First five even numbers are 2, 4, 6, 8, 19.. The nth even number is given by the formula 2*n. C++ // CPP program to fin
2 min read
C++ Program to Check Whether Number is Even or Odd
A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.ExamplesInput: n = 11Output: OddExplanation: Since 11 is not completely divisible by 2, it is an odd number.Inp
3 min read
C++ Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6
2 min read
C++ Program for Frequencies of even and odd numbers in a matrix
Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in the matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 1
3 min read
First element that appears even number of times in an array
Given an array, find the first element that appears even number of times in the array. It returns the element if exists otherwise returns 0. Examples: Input : arr[] = {1, 5, 4, 7, 4, 1, 5, 7, 1, 5}; Output : 4 Explanation, 4 is the first element that appears even number of times. Input : arr[] = {2,
7 min read
How to Find the Median of a Sorted Array in C++?
In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra
2 min read
How to Find the Median of Array in C++?
In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th
2 min read
C++ Program For Segregating Even And Odd Nodes In A Linked List
Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers the same. Examples: Input: 17->15->8->12->10->5->4->1->7->6-
7 min read
Missing even and odd elements from the given arrays
Given two integer arrays, even[] and odd[] which contain consecutive even and odd elements respectively, with one element missing from each of the arrays. The task is to find the missing elements. Examples: Input: even[] = {6, 4, 8, 14, 10}, odd[] = {7, 5, 3, 11, 13} Output: Even = 12 Odd = 9 Input:
13 min read