C++ Program to Print Even Numbers in an Array
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++
#include <iostream>
using namespace std;
int main()
{
int num1[] = { 2, 7, 6, 5, 4 };
int n = 5;
for ( int i = 0; i < n; i++) {
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++
#include <iostream>
using namespace std;
int main()
{
int num1[] = { 7, 5, 9, 6, 2, 3, 4 };
int n = 7;
int i = 0;
while (i < n) {
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++
#include <iostream>
using namespace std;
int main() {
int num1[] = { 2, 7, 6, 5, 4 };
int n = 5;
for ( int i = 0; i < n; i++) {
int x=num1[i]&1;
if (x==0) {
cout << num1[i] << " " ;
}
}
return 0;
}
|
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++
#include <iostream>
using namespace std;
int main() {
int num1[] = { 2, 7, 6, 5, 4 };
int n = 5;
for ( int i = 0; i < n; i++) {
if ((num1[i] | 1)==num1[i]+1) {
cout << num1[i] << " " ;
}
}
return 0;
}
|
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++
#include <iostream>
using namespace std;
void printEven( int * arr, int n, int index) {
if (index >= n) return ;
if (!(arr[index] & 1)) cout << arr[index] << " " ;
printEven(arr, n, ++index);
}
int main() {
int arr[] = { 2, 7, 6, 5, 4 };
int n = 5;
printEven(arr, n, 0);
return 0;
}
|
Time Complexity: O(N), as we are traversing entire array
Auxiliary Space: O(1), as constant extra space is used
Last Updated :
06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...