Program to print all the even numbers in an array
Take the array from the user, access each element using a loop. Check each element is even or not, dividing it by 2. If a number is completely divisible by 2 it is even else not.
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; //Size of array
cin>>n;
int a[n]; //array declaration
for(int i=0;i<n;i++){ //reading array
cin>>a[i];
}
for(int i=0;i<n;i++){ //loop to access each element of array
if(a[i]%2==0){ //if the array element is even print it
cout<<a[i]<<" ";
}
}
return 0;
}
Input
5
1 2 3 4 5
Output
2 4
0 Comments