Problem :- Given An Array A[], Write A C++ Program That Segregates Even And Odd Numbers. The Functions Should Put All Odd Numbers First, And Then Even Numbers .
Solution:-
#include<iostream>
using namespace std;
int main()
{
int a[100];
int n,i,j,left=0,right,temp;
cout<<"enter the size of array ";
cin>>n;
cout<<"\n enter the elelment of the array \n";
for(i=0;i<n;i++)
cin>>a[i];
right=n-1;
while(left<right)
{
if(a[left]%2!=0&left<right)
left++;
if(a[right]%2==0&&left<right)
right--;
if(a[right]%2!=0&&a[left]%2==0&&left<right)
{
temp=a[right];
a[right]=a[left];
a[left]=temp;
left++;
right--;
}
}
cout<<"segregates of array :";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output :-
Solution:-
#include<iostream>
using namespace std;
int main()
{
int a[100];
int n,i,j,left=0,right,temp;
cout<<"enter the size of array ";
cin>>n;
cout<<"\n enter the elelment of the array \n";
for(i=0;i<n;i++)
cin>>a[i];
right=n-1;
while(left<right)
{
if(a[left]%2!=0&left<right)
left++;
if(a[right]%2==0&&left<right)
right--;
if(a[right]%2!=0&&a[left]%2==0&&left<right)
{
temp=a[right];
a[right]=a[left];
a[left]=temp;
left++;
right--;
}
}
cout<<"segregates of array :";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output :-
0 Comments: