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++
// 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; } |
2 6 4
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; } |
6 2 4
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 |
2 6 4
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 |
2 6 4
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) |
2 6 4
Time Complexity: O(N), as we are traversing entire array
Auxiliary Space: O(1), as constant extra space is used
Please Login to comment...