Write C++ program to divide odd and even elements from array and store it in 2 different arrays.
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<conio>
#include<iostream>
void acceot();
void divide();
class array
{
int a[10],b[10],c[10],n,i,j,e; // a ,b & c= array
public:
void accept()
{
cout<<"\nEnter how number in an array : ";
cin>>n;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"\nEnter elements : ";
cin>>a[i];
cout<<endl;
}
}
void divide()
{
j=0;
e=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
b[j]=a[i];
j++;
}
else
{
c[j]=a[i];
e++;
}
}
// displaying elements in array
cout<<"\nArray is: "<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
cout<<"\nEven array is: "<<endl;
for(i=0;i<j;i++)
{
cout<<b[i]<<"\t";
}
cout<<endl;
cout<<"\nOdd array is: "<<endl;
for(i=0;i<e;i++)
{
cout<<c[i]<<"\t";
}
}
};
void main()
{
array a; // object created
a.accept();
a.divide();
getch();
}
0 Comments