28/10/2016

C++ Program For Segregate Even And Odd Numbers.

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 :-

                     
C++ Program For Segregates Even And Odd Numbers.
               
Previous Post
Next Post

post written by:

0 Comments: