Write a C++ program to find Even and Odd elements into the given array.
program Code:
#include<iostream>
using namespace std;
int main()
{
int arr[20],even[20],odd[20];
int i,j=0,k=0,n;
cout<<"How many elements you want to enter into the array : ";
cin>>n;
cout<<endl;
cout<<"Enter elements into the array: ";
for(i=0; i<n ;i++)
{
cin>>arr[i];
}
cout<<endl;
for(i=0; i<n;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
else
{
odd[k]=arr[i];
k++;
}
}
cout<<"Even elements into the array are : ";
for(i=0; i<j ;i++)
{
cout<<even[i]<<" ";
}
cout<<"Odd elements into the array are :: ";
for(i=0; i<k; i++)
{
cout<<odd[i]<<" ";
}
return 0;
}
The program output is tested on www.jdoodle.com
Output:
How many elements you want to enter into the array : 8
Enter elements into the array: 1 3 5 2 4 5 2 1
Even elements into the array are : 2 4 2
Odd elements into the array are :: 1 3 5 5 1
Thanks
Mukesh Rajput
Post A Comment:
0 comments: