Monday 10 February 2020

CPP program to display the odd and even elements separately in an array

Source Code:
#include<iostream>
using namespace std;
int main()
{
int c[10],even[10],odd[10],j=0,l=0,no;
cout<<"Enter the size of array: ";
cin>>no;
cout<<"Enter any "<<" "<<no<<" "<<"elements in Array: ";
for(int k=0;k<no;k++)
{
cin>>c[k];
}
for(int k=0;k<no;k++)
{
if(c[k]%2==0)
{
even[j]=c[k];
j++;
}
else
{
odd[l]=c[k];
l++;
}
}
cout<<"Even elements: ";
for(int k=0;k<j;k++)
{
cout<<even[k]<<" "<<endl;
}
cout<<"Odd elements: ";
for(int k=0;k<l;k++)
{
cout<<odd[k]<<" "<<endl;
}
return 0;
}

Program Output:

No comments:

Post a Comment