how to swap even elements to the right side and odd elements to the left side in an 1D array
NikhilMTomy:
What programming language are you using
Answers
Answered by
1
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i;
cout<<"Enter limit : ";
cin>>n;
cout<<"Enter elements of the array\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
for(j=i+;j<n;j++)
{
if(a[j]%2!=0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
cout<<"Array after arrangement \n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i;
cout<<"Enter limit : ";
cin>>n;
cout<<"Enter elements of the array\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
for(j=i+;j<n;j++)
{
if(a[j]%2!=0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
cout<<"Array after arrangement \n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}
Answered by
1
{
int temp,a[20],b[20];
for(i=0,m=0;i<n;i++)
for(j=n;j>0;)
{
if(a[i]%2==0)
{
b[m]==a[i];
m++;
}
else
{
b[j]=a[i];
j--;
}
}}
i think this would work
int temp,a[20],b[20];
for(i=0,m=0;i<n;i++)
for(j=n;j>0;)
{
if(a[i]%2==0)
{
b[m]==a[i];
m++;
}
else
{
b[j]=a[i];
j--;
}
}}
i think this would work
Similar questions
Math,
7 months ago
Math,
1 year ago
English,
1 year ago
Social Sciences,
1 year ago
Social Sciences,
1 year ago