Program to print all the odd numbers in an array
Take the array and array elements from the user, Using a loop check each element is even or odd. If the element is odd print the element. If the number is completely divisible by 2 it is even otherwise it is odd. For this modulo operator is used. To understand about modulo operator read modulo operator in C++.
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 odd print it
cout<<a[i]<<" ";
}
}
return 0;
}
Input
5
1 2 3 4 5
Output
1 3 5
0 Comments